Documentation ¶
Index ¶
- Variables
- type Client
- func (c *Client) Do(req *http.Request, v interface{}) (*http.Response, error)
- func (c *Client) NewRequest(method, urlStr string, body interface{}) (*http.Request, error)
- func (c *Client) NewRequestWithContext(ctx context.Context, method string, urlStr string, body interface{}) (*http.Request, error)
- type HTTPError
- type Headers
- type Options
Constants ¶
This section is empty.
Variables ¶
var ErrHTTP = errors.New("http error")
ErrHTTP define an http error
Functions ¶
This section is empty.
Types ¶
type Client ¶
type Client struct { BaseURL *url.URL DefaultHeaders Headers Host string // contains filtered or unexported fields }
Client basic structure
func New ¶
New function create a client using passed options BaseURL must be an HTTP or HTTPs absolute url and have a trailing slash
func (*Client) Do ¶
Do function executes http request using the passed request. This function automatically handles response in json to be decoded and saved into the `v` param.
func (*Client) NewRequest ¶
NewRequest function is same of NewRequestWithContext, without context
func (*Client) NewRequestWithContext ¶
func (c *Client) NewRequestWithContext(ctx context.Context, method string, urlStr string, body interface{}) (*http.Request, error)
NewRequestWithContext function works like the function of `net/http` package, simplified to a easier use with json request. Context and method are handled in the same way of core `net/http` package. Main differences are: * `urlString` params accept a string, that is converted to a type `url.URL` adding the client BaseURL (if provided in client options). To works correctly, urlString should not starts with `/` (otherwise BaseURL is not set) * body params in converted to a `json` buffer. If body is passed, the header `Content-Type: application/json` is automatically added to the request.
To the request are added all the DefaultHeaders (if body is passed, `application/json` content-type takes precedence over DefaultHeaders).