Documentation ¶
Overview ¶
Package httpjson provides facilities for transporting JSON encoded values HTTP message bodies.
Index ¶
- Variables
- func Do(ctx context.Context, method, url, contentType string, req, resp interface{}) error
- func Get(ctx context.Context, url string, v interface{}) error
- func IsJSONContentType(contentType string) bool
- func MarshalRequest(method, url, contentType string, v interface{}) (*http.Request, error)
- func UnmarshalRequest(req *http.Request, v interface{}) error
- func UnmarshalResponse(resp *http.Response, v interface{}) error
- func WriteResponse(w http.ResponseWriter, statusCode int, contentType string, v interface{}) error
- type Client
- type ResponseError
Constants ¶
This section is empty.
Variables ¶
var DefaultClient = &Client{}
DefaultClient is the client used by Get and Do.
Functions ¶
func Do ¶
Do creates and sends an HTTP request and processes the response. The request has the given method and is addressed to url, if req is not nil then it will be JSON encoded and used as the request body. The content type of the request is specified by contentType, which defaults to "application/json;charset=utf-8". If the HTTP request results in a valid response that is not a success the resulting error will be of type *ResponseError.
func Get ¶
Get retrieves a JSON document from the given URL and unmarshals the value into v. If the HTTP request results in a valid response that is not a success the resulting error will be of type *ResponseError.
func IsJSONContentType ¶
IsJSONContentType returns whether the given Content-Type is a JSON MIME Type as defined by the WHATWG MIME Sniffing Standard section 4.6 (https://mimesniff.spec.whatwg.org/#mime-type-groups).
func MarshalRequest ¶
MarshalRequest creates a new http.Request with the given method and URL and a body containing the JSON encoding of v.
If v is nil then the request will have no body. Otherwise v will be marshaled and then encoded using the character set specified by contentType. If the contentType is empty then the default contentType of "application/json;charset=utf-8" is used. If the contentType doesn't specify a character set then the value will be encoded as "us-ascii".
For a non-nil v the request will have the "Content-Length" and "Content-Type" headers set and include a GetBody method to support redirection.
func UnmarshalRequest ¶
UnmarshalRequest parses the JSON-encoded body of an http.Request and stores the result in the value pointed to by v.
UnmarshalRequest decodes the request body from the character set specified in the request's Content-Type header before parsing the JSON value.
func UnmarshalResponse ¶
UnmarshalResponse parses the JSON-encoded body of an http.Response and stores the result in the value pointed to by v.
UnmarshalResponse decodes the response body from the character set specified in the reponse's Content-Type header before parsing the JSON value.
func WriteResponse ¶
func WriteResponse(w http.ResponseWriter, statusCode int, contentType string, v interface{}) error
WriteResponse writes the JSON encoding of v as the body of an HTTP response.
The marshaled value will be encoded using the character set specified in the contentType. If the contentType is empty then the default contentType of "application/json;charset=utf-8" is used. If the contentType doesn't specify a character set then the value will be encoded as "us-ascii".
If v is nil then WriteResponse will write an empty body, otherwise WriteResponse will set the Content-Length and Content-Type headers before writing the response body.
If statusCode is > 0 then WriteResponse will call w.WriteHeader with the status code before writing the body.
Types ¶
type Client ¶
type Client struct { // HTTPClient is the http.Client to use for all HTTP requests. If // this is nil http.DefaultClient is used. HTTPClient *http.Client // IsJSONContentType is used to determine if an HTTP response // contains a JSON-encoded body. If this is nil the // IsJSONContentType function is used. IsJSONContentType func(contentType string) bool }
A Client is an HTTP client that transports JSON-encoded bodies. It's zero value (DefaultClient) is a usable client that uses http.DefaultClient.
func (*Client) Do ¶
func (c *Client) Do(ctx context.Context, method, url, contentType string, req, resp interface{}) error
Do creates and sends an HTTP request and processes the response. The request has the given method and is addressed to url, if req is not nil then it will be JSON encoded and used as the request body. The content type of the request is specified by contentType, which defaults to "application/json;charset=utf-8". If the HTTP request results in a valid response that is not a success the resulting error will be of type *ResponseError.
type ResponseError ¶
type ResponseError struct { // Response contains the http.Response that caused the error. The // Body field of this object will be nil and should be read from // the error's Body field. Response *http.Response // Body contains the body of the http Response that caused the // error. Body []byte }
A ResponseError is the error returned when the HTTP request returns a valid response that is either not a successful response, or is not a JSON content type.