httpjson

package module
v0.1.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jun 10, 2021 License: MIT Imports: 15 Imported by: 0

README

HTTPJSON

HTTPJSON is a package for sending and receiving JSON encoded objects in the bodies of HTTP messages.

Documentation

Overview

Package httpjson provides facilities for transporting JSON encoded values HTTP message bodies.

Index

Constants

This section is empty.

Variables

View Source
var DefaultClient = &Client{}

DefaultClient is the client used by Get and Do.

Functions

func Do

func 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.

func Get

func Get(ctx context.Context, url string, v interface{}) error

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

func IsJSONContentType(contentType string) bool

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

func MarshalRequest(method, url, contentType string, v interface{}) (*http.Request, error)

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

func UnmarshalRequest(req *http.Request, v interface{}) error

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

func UnmarshalResponse(resp *http.Response, v interface{}) error

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.

func (*Client) Get

func (c *Client) Get(ctx context.Context, url string, v interface{}) error

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.

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.

func (*ResponseError) Error

func (e *ResponseError) Error() string

Error implements error.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL