httputil

package
v1.7.30 Latest Latest
Warning

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

Go to latest
Published: Mar 29, 2019 License: MIT Imports: 25 Imported by: 19

Documentation

Overview

Utilities for extracting and formatting data encountered in HTTP requests

Index

Constants

View Source
const (
	Get     Method = `GET`
	Post           = `POST`
	Put            = `PUT`
	Delete         = `DELETE`
	Head           = `HEAD`
	Options        = `OPTIONS`
	Patch          = `PATCH`
)

Variables

View Source
var FormUnmarshalStructTag = `json`
View Source
var Logger = logging.MustGetLogger(`httputil`)

Functions

func AddQ

func AddQ(u *url.URL, key string, value interface{})

Appends a query string from then given url.URL

func DecodeResponse

func DecodeResponse(response *http.Response) (io.Reader, error)

Takes an http.Response and returns an io.Reader that will return the contents of the Response Body decoded according to the values (if any) of the Content-Encoding response header.

func DelQ

func DelQ(u *url.URL, key string)

Deletes a query string from then given url.URL

func EncodeBasicAuth added in v1.7.28

func EncodeBasicAuth(username string, password string) string

Encode the username and password into a value than can be used in the Authorization HTTP header.

func IsHttpErr

func IsHttpErr(err error) bool

func JSONDecoder

func JSONDecoder(in io.Reader, out interface{}) error

func JSONEncoder

func JSONEncoder(in interface{}) (io.Reader, error)

func LoadCertPool added in v1.6.25

func LoadCertPool(filename string) (*x509.CertPool, error)

Loads certificates from the given file and returns a usable x509.CertPool

func ParseFormRequest

func ParseFormRequest(req *http.Request, into interface{}) error

Parses the form values for a Request and unmarshals into the given value.

func ParseFormValues

func ParseFormValues(formValues url.Values, into interface{}) error

Parses a set of values received from an HTML form (usually the value of the http.Request.Form property) and unmarshals into the given value.

func ParseJSON

func ParseJSON(r io.Reader, into interface{}) error

Parses a given reader as a JSON document and unmarshals into the given value.

func ParseJSONRequest

func ParseJSONRequest(req *http.Request, into interface{}) error

Parses the Request as JSON and unmarshals into the given value.

func ParseRequest

func ParseRequest(req *http.Request, into interface{}) error

Autodetect the Content-Type of the given request and unmarshals into the given value.

func Q

func Q(req *http.Request, key string, fallbacks ...string) string

Parses the named query string from a request as a string.

func QBool

func QBool(req *http.Request, key string, fallbacks ...bool) bool

Parses the named query string from a request as a boolean value.

func QDuration added in v1.6.23

func QDuration(req *http.Request, key string) time.Duration

Parses the named query string from a request as a duration string.

func QFloat

func QFloat(req *http.Request, key string, fallbacks ...float64) float64

Parses the named query string from a request as a float.

func QInt

func QInt(req *http.Request, key string, fallbacks ...int64) int64

Parses the named query string from a request as an integer.

func QStrings added in v1.5.58

func QStrings(req *http.Request, key string, delimiter string, fallbacks ...string) []string

Parses the named query string from a request as a delimiter-separated string slice.

func QTime

func QTime(req *http.Request, key string) time.Time

Parses the named query string from a request as a date/time value.

func RespondJSON

func RespondJSON(w http.ResponseWriter, data interface{}, status ...int)

Marshal the given data as a JSON document and write the output to the given ResponseWriter. If a status is given, that will be used as the HTTP response status. If data is an error, and no status is given, the status will be "500 Internal Server Error"; if data is nil, the status will be "204 No Content". The Content-Type of the response is "application/json".

func SetContentTypeParser

func SetContentTypeParser(contentType string, parser RequestParseFunc)

Sets a parser implementation for the given HTTP content type.

func SetQ

func SetQ(u *url.URL, key string, value interface{})

Sets a query string to the given value in the given url.URL

func SetRootCABundle added in v1.6.24

func SetRootCABundle(client *http.Client, caBundle string) error

Configures the given http.Client to accept TLS certificates validated by the given PEM-encoded CA bundle file

Types

type Client

type Client struct {
	// contains filtered or unexported fields
}

func NewClient

func NewClient(baseURI string) (*Client, error)

func (*Client) AppendTrustedRootCA added in v1.7.25

func (self *Client) AppendTrustedRootCA(pemFilenamesOrData ...interface{}) error

Append one or more trusted certificates to the RootCA bundle that is consulted when performing HTTPS requests.

func (*Client) ClearHeaders

func (self *Client) ClearHeaders()

Remove all implicit HTTP request headers.

func (*Client) ClearParams

func (self *Client) ClearParams()

Remove all implicit querystring parameters.

func (*Client) Client

func (self *Client) Client(*http.Client) *http.Client

Returns the HTTP client used to perform requests

func (*Client) Decode

func (self *Client) Decode(r io.Reader, out interface{}) error

Decode a response and, if applicable, automatically close the reader.

func (*Client) Delete

func (self *Client) Delete(path string, params map[string]interface{}, headers map[string]interface{}) (*http.Response, error)

func (*Client) Encode

func (self *Client) Encode(in interface{}) ([]byte, error)

func (*Client) Get

func (self *Client) Get(path string, params map[string]interface{}, headers map[string]interface{}) (*http.Response, error)

func (*Client) GetWithBody

func (self *Client) GetWithBody(path string, body interface{}, params map[string]interface{}, headers map[string]interface{}) (*http.Response, error)

func (*Client) Post

func (self *Client) Post(path string, body interface{}, params map[string]interface{}, headers map[string]interface{}) (*http.Response, error)

func (*Client) Put

func (self *Client) Put(path string, body interface{}, params map[string]interface{}, headers map[string]interface{}) (*http.Response, error)

func (*Client) Request

func (self *Client) Request(
	method Method,
	path string,
	body interface{},
	params map[string]interface{},
	headers map[string]interface{},
) (*http.Response, error)

Perform an HTTP request

func (*Client) SetBasicAuth added in v1.7.28

func (self *Client) SetBasicAuth(username string, password string)

Set the username and password to be included in the Authorization header.

func (*Client) SetClient

func (self *Client) SetClient(client *http.Client)

Replace the default HTTP client with a user-provided one

func (*Client) SetDecoder

func (self *Client) SetDecoder(fn DecoderFunc)

Specify a decoder that will be used to deserialize data in the response body.

func (*Client) SetEncoder

func (self *Client) SetEncoder(fn EncoderFunc)

Specify an encoder that will be used to serialize data in the request body.

func (*Client) SetErrorDecoder

func (self *Client) SetErrorDecoder(fn ErrorDecoderFunc)

Specify a different decoder used to deserialize non 2xx/3xx HTTP responses.

func (*Client) SetHeader

func (self *Client) SetHeader(name string, value interface{})

Add an HTTP request header by name that will be included in every request. If value is nil, the named header will be removed instead.

func (*Client) SetInsecureTLS added in v1.7.26

func (self *Client) SetInsecureTLS(insecure bool)

Set or unset insecure TLS requests that will proceed even if the peer certificate cannot be verified.

func (*Client) SetParam

func (self *Client) SetParam(name string, value interface{})

Add a querystring parameter by name that will be included in every request. If value is nil, the parameter will be removed instead.

func (*Client) SetPostRequestHook

func (self *Client) SetPostRequestHook(fn InterceptResponseFunc)

Specify a function tht will be called immediately after a response is received. This function is given the first opportunity to inspect the response, and if it returns a non-nil error, no additional processing (including the Error Decoder function) will be performed.

func (*Client) SetPreRequestHook

func (self *Client) SetPreRequestHook(fn InterceptRequestFunc)

Specify a function that will be called immediately before a request is sent. This function has an opportunity to read and modify the outgoing request, and if it returns a non-nil error, the request will not be sent.

func (*Client) SetRootCA added in v1.7.25

func (self *Client) SetRootCA(pemFilenamesOrData ...interface{}) error

Replace the existing RootCA bundle with an explicit set of trusted certificates.

func (*Client) URI

func (self *Client) URI() *url.URL

Return the base URI for this client.

type DecoderFunc

type DecoderFunc func(io.Reader, interface{}) error

type EncoderFunc

type EncoderFunc func(interface{}) (io.Reader, error)

type ErrorDecoderFunc

type ErrorDecoderFunc func(*http.Response) error

type InterceptRequestFunc

type InterceptRequestFunc func(*http.Request) (interface{}, error)

type InterceptResponseFunc

type InterceptResponseFunc func(*http.Response, interface{}) error

type LogLevel

type LogLevel int
const (
	Debug LogLevel = iota
	Info
	Notice
	Warning
	Error
	Fatal
)

type Method

type Method string

type RequestLogger

type RequestLogger struct {
}

func NewRequestLogger

func NewRequestLogger() *RequestLogger

func (*RequestLogger) ServeHTTP

func (self *RequestLogger) ServeHTTP(rw http.ResponseWriter, req *http.Request, next http.HandlerFunc)

type RequestParseFunc

type RequestParseFunc func(*http.Request, interface{}) error

type WritableLogger

type WritableLogger struct {
	// contains filtered or unexported fields
}

func NewWritableLogger

func NewWritableLogger(level LogLevel, prefix ...string) *WritableLogger

func (*WritableLogger) Write

func (self *WritableLogger) Write(p []byte) (int, error)

Jump to

Keyboard shortcuts

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