ezhttp

package
v2.14.7 Latest Latest
Warning

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

Go to latest
Published: Apr 20, 2024 License: MIT Imports: 14 Imported by: 1

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func DecodeJSON

func DecodeJSON(r io.Reader, v any) error

DecodeJSON decodes a json value from r.

func DecodeXML

func DecodeXML(r io.Reader, v any) error

DecodeXML decodes a XML value from r.

func Do

func Do(req *Request) (header http.Header, respContent []byte, status int, err error)

Do is a convenient function to send request and control redirect and debug options. If `Request.Resp` is provided, it will be used as destination to try to unmarshal the response body.

Trade-off was taken to balance simplicity and convenience.

For more powerful controls of a http request and handy utilities, you may take a look at the awesome library `https://github.com/go-resty/resty/`.

func IsJSONType

func IsJSONType(contentType string) bool

IsJSONType method is to check JSON content type or not.

func IsXMLType

func IsXMLType(contentType string) bool

IsXMLType method is to check XML content type or not.

func JSON added in v2.3.2

func JSON(w http.ResponseWriter, statusCode int, data any)

JSON serializes the given data as JSON into the response body. It also sets the Content-Type as "application/json".

func JSONHumanFriendly added in v2.3.2

func JSONHumanFriendly(w http.ResponseWriter, statusCode int, data any)

JSONHumanFriendly serializes the given data as pretty JSON (indented + newlines) into the response body. It also sets the Content-Type as "application/json".

WARNING: we recommend using this only for development purposes since printing pretty JSON is more CPU and bandwidth consuming. Use JSON() instead.

Types

type Request

type Request struct {

	// Req should be a fully prepared http Request to sent, if not nil,
	// the following `Method`, `URL`, `Params`, `JSON`, `XML`, `Form`, `Body`
	// will be ignored.
	//
	// If Req is nil, it will be filled using the following data `Method`,
	// `URL`, `Params`, `JSON`, `XML`, `Form`, `Body` to construct the `http.Request`.
	//
	// When building http body, the priority is JSON > XML > Form > Body.
	Req *http.Request

	// Method specifies the verb for the http request, it's optional,
	// default is "GET".
	Method string

	// URL specifies the url to make the http request.
	// It's required if Req is nil.
	URL string

	// Params specifies optional params to merge with URL, it must be one of
	// the following types:
	// - map[string]string
	// - map[string][]string
	// - map[string]any
	// - url.Values
	Params any

	// JSON specifies optional body data for request which can take body,
	// the content-type will be "application/json", it must be one of
	// the following types:
	// - io.Reader
	// - []byte (will be wrapped with bytes.NewReader)
	// - string (will be wrapped with strings.NewReader)
	// - any (will be marshaled with json.Marshal)
	JSON any

	// XML specifies optional body data for request which can take body,
	// the content-type will be "application/xml", it must be one of
	// the following types:
	// - io.Reader
	// - []byte (will be wrapped with bytes.NewReader)
	// - string (will be wrapped with strings.NewReader)
	// - any (will be marshaled with xml.Marshal)
	XML any

	// Form specifies optional body data for request which can take body,
	// the content-type will be "application/x-www-form-urlencoded",
	// it must be one of the following types:
	// - io.Reader
	// - []byte (will be wrapped with bytes.NewReader)
	// - string (will be wrapped with strings.NewReader)
	// - url.Values (will be encoded and wrapped as io.Reader)
	// - map[string]string (will be converted to url.Values)
	// - map[string][]string (will be converted to url.Values)
	// - map[string]any (will be converted to url.Values)
	Form any

	// Body specifies optional body data for request which can take body,
	// the content-type will be detected from the content (may be incorrect),
	// it should be one of the following types:
	// - io.Reader
	// - []byte (will be wrapped with bytes.NewReader)
	// - string (will be wrapped with strings.NewReader)
	Body any

	// Headers will be copied to the request before sent.
	//
	// If "Content-Type" presents, it will replace the default value
	// set by `JSON`, `XML`, `Form`, or `Body`.
	Headers map[string]string

	// Resp specifies an optional destination to unmarshal the response data.
	// if `Unmarshal` is not provided, the header "Content-Type" will be used to
	// detect XML content, else `json.Unmarshal` will be used.
	Resp any

	// Unmarshal specifies an optional function to unmarshal the response data.
	Unmarshal func([]byte, any) error

	// Context specifies an optional context.Context to use with http request.
	Context context.Context

	// Timeout specifies an optional timeout of the http request, if
	// timeout > 0, the request will be attached an timeout context.Context.
	// Timeout takes higher priority than Context, if both available, only
	// `Timeout` takes effect.
	Timeout time.Duration

	// Client specifies an optional http.Client to do the request, instead of
	// the default http client.
	Client *http.Client

	// DisableRedirect tells the http client don't follow response redirection.
	DisableRedirect bool

	// CheckRedirect specifies the policy for handling redirects.
	// See http.Client.CheckRedirect for details.
	//
	// CheckRedirect takes higher priority than DisableRedirect,
	// if both available, only `CheckRedirect` takes effect.
	CheckRedirect func(req *http.Request, via []*http.Request) error

	// DiscardResponseBody makes the http response body being discarded.
	DiscardResponseBody bool

	// DumpRequest makes the http request being logged before sent.
	DumpRequest bool

	// DumpResponse makes the http response being logged after received.
	DumpResponse bool

	// When DumpRequest or DumpResponse is true, or both are true,
	// DumpFunc optionally specifies a function to dump the request and response,
	// by default, [log.Printf] is used.
	DumpFunc func(format string, args ...any)

	// RaiseForStatus tells Do to report an error if the response
	// status code >= 400. The error will be formatted as "unexpected status: <STATUS>".
	RaiseForStatus bool
	// contains filtered or unexported fields
}

Request represents a request and options to send with the Do function.

func (*Request) SetBasicAuth added in v2.10.3

func (p *Request) SetBasicAuth(username, password string) *Request

SetBasicAuth sets the request's Authorization header to use HTTP Basic Authentication with the provided username and password.

With HTTP Basic Authentication the provided username and password are not encrypted. It should generally only be used in an HTTPS request.

See http.Request.SetBasicAuth for details.

Jump to

Keyboard shortcuts

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