Documentation
¶
Overview ¶
Package httpz provides functionality supplemental to stdlib http. Indeed, some of the functions are copied verbatim from stdlib. The jumping-off point is httpz.NewClient.
Design note: this package contains generally fairly straightforward HTTP functionality, but the Opt / TripFunc config mechanism is a bit experimental. And probably tries to be a bit too clever. It may change.
And one last thing: remember kids, ALWAYS close your response bodies.
Index ¶
- Variables
- func Filename(resp *http.Response) string
- func NewClient(opts ...Opt) *http.Client
- func NewDefaultClient() *http.Client
- func NopTripFunc(next http.RoundTripper, req *http.Request) (*http.Response, error)
- func ReadResponseHeader(r *bufio.Reader, req *http.Request) (resp *http.Response, err error)
- func RequestLogValue(req *http.Request) slog.Value
- func ResponseLogValue(resp *http.Response) slog.Value
- func RoundTrip(next http.RoundTripper, fn TripFunc) http.RoundTripper
- func StatusText(code int) string
- type Opt
- type OptInsecureSkipVerify
- type TripFunc
Constants ¶
This section is empty.
Variables ¶
var DefaultHeaderTimeout = OptRequestTimeout(time.Second * 5)
DefaultHeaderTimeout is the default header timeout as used by NewDefaultClient.
var DefaultTLSVersion = minTLSVersion(tls.VersionTLS10)
DefaultTLSVersion is the default minimum TLS version, as used by NewDefaultClient.
var DefaultUserAgent = OptUserAgent(buildinfo.Get().UserAgent())
DefaultUserAgent is the default User-Agent header value, as used by NewDefaultClient.
Functions ¶
func Filename ¶
Filename returns the filename to use for a download. It first checks the Content-Disposition header, and if that's not present, it uses the last path segment of the URL. The filename is sanitized. It's possible that the returned value will be empty string; the caller should handle that situation themselves.
func NewDefaultClient ¶
NewDefaultClient invokes NewClient with default settings.
func NopTripFunc ¶
NopTripFunc is a TripFunc that does nothing.
func ReadResponseHeader ¶
ReadResponseHeader is a fork of http.ReadResponse that reads only the header from req and not the body. Note that resp.Body will be nil, and that the resp object is borked for general use.
func RequestLogValue ¶
RequestLogValue implements slog.LogValuer for http.Request.
func ResponseLogValue ¶
ResponseLogValue implements slog.LogValuer for http.Response.
func RoundTrip ¶
func RoundTrip(next http.RoundTripper, fn TripFunc) http.RoundTripper
RoundTrip adapts a TripFunc to http.RoundTripper.
func StatusText ¶
StatusText is like http.StatusText, but also includes the code, e.g. "200 OK".
Types ¶
type Opt ¶
type Opt interface {
// contains filtered or unexported methods
}
Opt is an option that can be passed to NewClient to configure the client.
type OptInsecureSkipVerify ¶
type OptInsecureSkipVerify bool
OptInsecureSkipVerify is an Opt that can be passed to NewClient that, when true, disables TLS verification.
type TripFunc ¶
TripFunc is a function that implements http.RoundTripper. It is commonly used with RoundTrip to decorate an existing http.RoundTripper.
func OptRequestDelay ¶
OptRequestDelay is passed to NewClient to delay the request by the specified duration. This is useful for testing.
func OptRequestTimeout ¶
OptRequestTimeout is passed to NewClient to set a timeout for just getting the initial response headers. This is useful if you expect a response within, say, 2 seconds, but you expect the body to take longer to read.
Contrast with OptResponseTimeout.
func OptResponseTimeout ¶
OptResponseTimeout is passed to NewClient to set the total request timeout, including reading the body. This is basically the same as a traditional request timeout via context.WithTimeout. If timeout is zero, this is no-op.
Contrast with OptRequestTimeout.
func OptUserAgent ¶
OptUserAgent is passed to NewClient to set the User-Agent header.