request

package
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: May 31, 2024 License: MIT Imports: 11 Imported by: 0

Documentation

Index

Constants

View Source
const AcceptStatusHeader = "X-Blugnu-Http-Accept-Status"

canonical casing avoids go-staticcheck flagging the constant with SA1008

View Source
const MaxRetriesHeader = "X-Blugnu-Http-Max-Retries"

canonical casing avoids go-staticcheck flagging the constant with SA1008

View Source
const ResponseBodyRequiredHeader = "X-Blugnu-Http-Response-Body-Required"

canonical casing avoids go-staticcheck flagging the constant with SA1008

View Source
const StreamResponseHeader = "X-Blugnu-Http-Stream-Response"

canonical casing avoids go-staticcheck flagging the constant with SA1008

Variables

View Source
var (
	ErrInvalidJSON      = errors.New("invalid json")
	ErrMarshallingJSON  = errors.New("error marshalling json")
	ErrSetBoundary      = errors.New("SetBoundary error")
	ErrTooManyArguments = errors.New("too many arguments")
	ErrInvalidQuery     = errors.New("invalid query")
)
View Source
var ErrCopyFailed = errors.New("copy() operation failed or was incomplete")

ErrCopyFailed is the error returned by the Body() RequestOption if the supplied byte slice cannot be completely copied to the request.Body.

Functions

func Accept

func Accept(contentType string) func(rq *http.Request) error

Accept sets the canonical Accept header on a request

func AcceptJSON

func AcceptJSON() func(rq *http.Request) error

Accept sets the canonical Accept header with a value of "application/json"

func AcceptStatus

func AcceptStatus(statusCodes ...int) func(*http.Request) error

func BearerToken

func BearerToken(fn func(context.Context) (string, error)) func(*http.Request) error

BearerToken sets a canonical Authorisation header with a BearerToken value, using the result of a provided function.

The token value is not supplied directly; instead, the provided function will be called to obtain a token, or an error if a token is not available.

func Body

func Body(data []byte) func(*http.Request) error

Body sets the body of a request to the contents of a supplied byte slice and the ContentLength to the length of the slice.

request.ErrCopyFailed is returned if the provided slice cannot be completely copied to the request Body.

func ContentType

func ContentType(s string) func(*http.Request) error

ContentType sets the canonical Content-Type header on a request.

func Header(k, v string) func(*http.Request) error

Header sets the value of a canonical header.

Canonical header keys are normalised; normalising a non-canonical header may result in unintended changes to the case of the key.

To set a non-canonical header use RawHeader() instead.

Example:

// sets the canonical "Content-Type" header
Header("content-type", "application/json")

func JSONBody

func JSONBody(v any) func(*http.Request) error

JSONBody sets the body of a request to the contents of a supplied value marshalled as JSON. A Content-Type header is added with the value application/json. The ContentLength is also set to the length of the JSON encoded bytes.

func MaxRetries

func MaxRetries(n uint) func(*http.Request) error

MaxRetries configures a maximum number of retries on a specific request. If set, this overrides any MaxRetries that may be configured on the client used to make the request.

e.g. if the client is configured with MaxRetries == 5 and a request is submitted with MaxRetries == 3, then at most 4 attempts will be made: the initial request and at most 3 retry attempts

func MultipartFormDataFromMap

func MultipartFormDataFromMap[K comparable, V any](
	m map[K]V,
	opts ...func(multipart.Options),
) func(*http.Request) error

MultipartFormDataFromMap configures a multipart form data body by mapping the items in a map to the parts of the form data.

A map must be provided together with a function to provide the field id, filename and content bytes of each part, given the key and value of an item in the map (or an error if a valid part is unable to be derived).

The parts are added in order of map keys, after those keys have been sorted.

func NonCanonicalHeader

func NonCanonicalHeader(k, v string) func(*http.Request) error

NonCanonicalHeader sets the value of a header without canonicalising the key.

When setting a NonCanonicalHeader() the key is applied exactly as-specified; this may be important for non-canonical keys but is undesirable for canonical headers.

If setting a canonical header, use Header() instead.

Example:

// sets a non-canonical "sessionid" header
NonCanonicalHeader("sessionid", id)

func Query

func Query(params map[string]any) func(*http.Request) error

Query adds all key-value pairs in a supplied map to the query of a request.

Keys are url encoded before being added to the query. If any value is nil the corresponding key is added to the query with no value:

request.Query(map[string]any{"foo", nil}) -> ?foo

If the value is not nil it also is url encoded before being added to the query:

request.Query(map[string]any{"foo", true}) -> ?foo=true
request.Query(map[string]any{"foo", "subkey:value"}) -> ?foo=subkey%3Avalue

The order of the keys in the resulting query is not guaranteed as a result of map iteration order being undefined in Go. If key order is important then QueryP should be called to add each key-value pair individually in the desired order.

func QueryP

func QueryP(k string, v any) func(*http.Request) error

QueryP adds a single key-value pair (or Param) to the query of a request.

The key is url encoded before being added to the query. If the value is nil the key is added to the query with no value:

request.QueryP("foo", nil) -> ?foo

If the value is not nil it is url encoded before being added to the query:

request.QueryP("foo", true) -> ?foo=true
request.QueryP("'a map'", "key=value") -> ?%27a+map%27=key%3Dvalue

func RawQuery

func RawQuery(s string) func(*http.Request) error

RawQuery sets the query string of a request. Any existing query string will be overwritten.

The string is expected to be a valid, url encoded string.

func ResponseBodyRequired

func ResponseBodyRequired() func(*http.Request) error

ResponseBodyRequired establishes that a non-empty response body is expected in response to this request. If the response provides an empty body the client will return an http.ErrNoResponseBody error, together with the response

func StreamResponse

func StreamResponse() func(*http.Request)

StreamResponse adds a request header indicating that the client expects to stream the response body. The header is removed

If specified, the usual reading of the response body prior to returning the response to the caller is skipped.

Types

This section is empty.

Jump to

Keyboard shortcuts

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