Documentation ¶
Overview ¶
Package httpx includes various HTTP utilities.
Index ¶
- func Copy(w http.ResponseWriter, res *http.Response) error
- func DecodeDeepObject(values url.Values, obj any) error
- func EncodeDeepObject(obj any) (url.Values, error)
- func ExtractPathArguments(spec string, path string) (args url.Values, err error)
- func FillPathArguments(u string) (resolved string, err error)
- func InsertPathArguments(u string, values QArgs) string
- func IsLocalhostAddress(r *http.Request) bool
- func IsPrivateIPAddress(r *http.Request) bool
- func JoinHostAndPath(host string, path string) string
- func MustNewRequest(method string, url string, body any) *http.Request
- func MustNewRequestWithContext(ctx context.Context, method string, url string, body any) *http.Request
- func NewRequest(method string, url string, body any) (*http.Request, error)
- func NewRequestWithContext(ctx context.Context, method string, url string, body any) (*http.Request, error)
- func ParseRequestBody(r *http.Request, data any) error
- func ParseRequestData(r *http.Request, data any) error
- func ParseURL(rawURL string) (canonical *url.URL, err error)
- func ResolveURL(base string, relative string) (resolved string, err error)
- func SetRequestBody(r *http.Request, body any) error
- type BodyReader
- type DefragRequest
- type DefragResponse
- type FragRequest
- type FragResponse
- type QArgs
- type ResponseRecorder
- func (rr *ResponseRecorder) Clear()
- func (rr *ResponseRecorder) ContentLength() int
- func (rr *ResponseRecorder) Header() http.Header
- func (rr *ResponseRecorder) Result() *http.Response
- func (rr *ResponseRecorder) StatusCode() int
- func (rr *ResponseRecorder) Write(b []byte) (int, error)
- func (rr *ResponseRecorder) WriteHeader(statusCode int)
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func Copy ¶
func Copy(w http.ResponseWriter, res *http.Response) error
Copy writes the http.Response to the http.ResponseWriter. Do not modify the body of the http.Response after calling this methog. http.ResponseWriter is assumed to be empty.
func DecodeDeepObject ¶
DecodeDeepObject decodes an object from a string representation with bracketed nested fields names. For example, color[R]=100&color[G]=200&color[B]=150 .
func EncodeDeepObject ¶
EncodeDeepObject encodes an object into string representation with bracketed nested fields names. For example, color[R]=100&color[G]=200&color[B]=150 .
func ExtractPathArguments ¶
ExtractPathArguments extracts path arguments from a URL or path given a spec such as /obj/{id}/{} that identified them. Unnamed args are assigned the names path1, path2, etc.
func FillPathArguments ¶
FillPathArguments transfers query arguments into path arguments, if present.
func InsertPathArguments ¶
InsertPathArguments fills URL path arguments such as {arg} from the named value. If the argument is not named, e.g. {}, then a default name of path1, path2, etc. is assumed.
func IsLocalhostAddress ¶
IsLocalhostAddress checks if the request's remote address is the local host.
func IsPrivateIPAddress ¶
IsPrivateIPAddress checks if the request's remote address is on the local subnets, e.g. 192.168.X.X or 10.X.X.X.
func JoinHostAndPath ¶
JoinHostAndPath combines the path shorthand with a hostname.
func MustNewRequest ¶
MustNewRequest wraps NewRequestWithContext with the background context. It panics on error.
func MustNewRequestWithContext ¶
func MustNewRequestWithContext(ctx context.Context, method string, url string, body any) *http.Request
MustNewRequestWithContext returns a new http.Request given a method, URL, and optional body. It panics on error. Arguments of type io.Reader, io.ReadCloser, []byte and string are serialized in binary form. url.Values and QArgs are serialized as form data. All other types are serialized as JSON. The Content-Type Content-Length headers will be set to match the body if they can be determined and unless already set.
func NewRequest ¶
NewRequest wraps NewRequestWithContext with the background context.
func NewRequestWithContext ¶
func NewRequestWithContext(ctx context.Context, method string, url string, body any) (*http.Request, error)
NewRequestWithContext returns a new http.Request given a method, URL, and optional body. Arguments of type io.Reader, io.ReadCloser, []byte and string are serialized in binary form. url.Values and QArgs are serialized as form data. All other types are serialized as JSON. The Content-Type Content-Length headers will be set to match the body if they can be determined and unless already set.
func ParseRequestBody ¶
ParseRequestBody parses the body of an incoming request and populates the fields of a data object. It supports JSON and URL-encoded form data content types. Use json tags to designate the name of the argument to map to each field.
func ParseRequestData ¶
ParseRequestData parses the body and query arguments of an incoming request and populates the fields of a data object. Use json tags to designate the name of the argument to map to each field. An argument name can be hierarchical using either notation "a[b][c]" or "a.b.c", in which case it is read into the corresponding nested field.
func ParseURL ¶
ParseURL returns a canonical version of the parsed URL with the scheme and port filled in if omitted.
func ResolveURL ¶
ResolveURL resolves a URL in relation to the endpoint's base path.
func SetRequestBody ¶
SetRequestBody sets the body of the request. Arguments of type io.Reader, io.ReadCloser, []byte and string are serialized in binary form. url.Values and QArgs are serialized as form data. All other types are serialized as JSON. The Content-Type Content-Length headers will be set to match the body if they can be determined and unless already set.
Types ¶
type BodyReader ¶
type BodyReader struct {
// contains filtered or unexported fields
}
BodyReader is used to wrap bytes in a closer+reader while allowing access to the underlying bytes.
func NewBodyReader ¶
func NewBodyReader(b []byte) *BodyReader
NewBodyReader creates a new closer+reader for the request body while allowing access to the underlying bytes.
func (*BodyReader) Bytes ¶
func (br *BodyReader) Bytes() []byte
Bytes gives access to the underlying bytes.
func (*BodyReader) Close ¶
func (br *BodyReader) Close() error
Read implements the io.Closer interface.
func (*BodyReader) Read ¶
func (br *BodyReader) Read(p []byte) (n int, err error)
Read implements the io.Reader interface.
func (*BodyReader) Reset ¶
func (br *BodyReader) Reset()
Reset resets the underlying reader to the beginning.
type DefragRequest ¶
type DefragRequest struct {
// contains filtered or unexported fields
}
DefragRequest merges together multiple fragments back into a single HTTP request
func NewDefragRequest ¶
func NewDefragRequest() *DefragRequest
NewDefragRequest creates a new request integrator.
func (*DefragRequest) Add ¶
func (st *DefragRequest) Add(r *http.Request) error
Add a fragment to be integrated.
func (*DefragRequest) Integrated ¶
func (st *DefragRequest) Integrated() (integrated *http.Request, err error)
Integrated indicates if all the fragments have been collected and if so returns them as a single HTTP request.
func (*DefragRequest) LastActivity ¶
func (st *DefragRequest) LastActivity() time.Duration
LastActivity indicates how long ago was the last fragment added.
type DefragResponse ¶
type DefragResponse struct {
// contains filtered or unexported fields
}
DefragResponse merges together multiple fragments back into a single HTTP response
func NewDefragResponse ¶
func NewDefragResponse() *DefragResponse
NewDefragResponse creates a new response integrator.
func (*DefragResponse) Add ¶
func (st *DefragResponse) Add(r *http.Response) error
Add a fragment to be integrated.
func (*DefragResponse) Integrated ¶
func (st *DefragResponse) Integrated() (integrated *http.Response, err error)
Integrated indicates if all the fragments have been collected and if so returns them as a single HTTP response.
func (*DefragResponse) LastActivity ¶
func (st *DefragResponse) LastActivity() time.Duration
LastActivity indicates how long ago was the last fragment added.
type FragRequest ¶
type FragRequest struct {
// contains filtered or unexported fields
}
FragRequest transforms an HTTP request into one or more fragments that do not exceed a given size. Fragmenting is needed because NATS imposes a maximum size for messages
func NewFragRequest ¶
func NewFragRequest(r *http.Request, fragmentSize int64) (*FragRequest, error)
NewFragRequest creates a new request fragmentor
type FragResponse ¶
type FragResponse struct {
// contains filtered or unexported fields
}
FragResponse transforms an HTTP response into one or more fragments that do not exceed a given size. Fragmenting is needed because NATS imposes a maximum size for messages
func NewFragResponse ¶
func NewFragResponse(r *http.Response, fragmentSize int64) (*FragResponse, error)
NewFragResponse creates a new response fragmentor
type QArgs ¶
QArgs faciliates the creation of URL query arguments for the common case where there is only one value per key. Values of any type are converted to a string. Keys are case-sensitive.
Usage:
u := "https://example.com/path?"+QArgs{ "hello": "World", "number": 6, "id", key }.Encode()
func (QArgs) Encode ¶
Encode encodes the values into “URL encoded” form ("bar=baz&foo=quux") sorted by key.
type ResponseRecorder ¶
type ResponseRecorder struct {
// contains filtered or unexported fields
}
ResponseRecorder is used to record HTTP responses.
func NewResponseRecorder ¶
func NewResponseRecorder() *ResponseRecorder
NewResponseRecorder creates a new response recorder. The recorder is modifiable even after the body was written because it keeps the entire response in memory.
func (*ResponseRecorder) Clear ¶
func (rr *ResponseRecorder) Clear()
Clear deletes all the content recorded to date.
func (*ResponseRecorder) ContentLength ¶
func (rr *ResponseRecorder) ContentLength() int
ContentLength returns the total number of bytes written to the body of the response.
func (*ResponseRecorder) Header ¶
func (rr *ResponseRecorder) Header() http.Header
Header enables setting headers. It implements the http.ResponseWriter interface.
func (*ResponseRecorder) Result ¶
func (rr *ResponseRecorder) Result() *http.Response
Result returns the response generated by the recorder.
func (*ResponseRecorder) StatusCode ¶
func (rr *ResponseRecorder) StatusCode() int
StatusCode returns the status code set for the response. If unset, the default is http.StatusOK 200.
func (*ResponseRecorder) Write ¶
func (rr *ResponseRecorder) Write(b []byte) (int, error)
Write writes bytes to the body of the response. It implements the http.ResponseWriter interface.
func (*ResponseRecorder) WriteHeader ¶
func (rr *ResponseRecorder) WriteHeader(statusCode int)
WriteHeader writes the header to the response. It implements the http.ResponseWriter interface.