http
v0.4.1
Documentation
pkg.go.dev ↗Package http provides an HTTP/1.1 client with deadline enforcement, connection pooling, and error classification.
Types
Client
type Client struct {
// contains filtered or unexported fields
}
Client is an HTTP/1.1 client with connection pooling and deadline support.
New
func New(options Options) *Client
New creates a new HTTP Client with the given options.
Client.Get
func (c *Client) Get(href string, deadline time.Time) (*Response, error)
Get sends a GET for href, a serialized http(s) URL without userinfo or fragment, and returns once the headers arrive. deadline covers the whole attempt, body included. Closing the response releases its resources. URLs containing userinfo, control characters, an unsupported scheme, or invalid format are rejected with KindOther.
Error
type Error struct {
Kind Kind
Err error
// contains filtered or unexported fields
}
Error is every failure Get and Body return.
Error.Error
func (e *Error) Error() string
Error.Status
func (e *Error) Status() (int, bool)
Status returns the HTTP status code and true if headers were received, or 0 and false if not.
Error.Unwrap
func (e *Error) Unwrap() error
Kind
type Kind uint8
Kind classifies the outcome of a failed HTTP request.
KindOther (9 declarations)
const ( // KindOther is an unclassified or caller error. KindOther Kind = iota // KindTimeout indicates the deadline expired before completion. KindTimeout // KindDNS indicates a DNS resolution failure. KindDNS // KindConnect indicates a failure to establish a TCP connection. KindConnect // KindTLS indicates a TLS handshake failure. KindTLS // KindReset indicates an unexpected connection termination or reset. KindReset // KindMalformed indicates an invalid or protocol-violating response. KindMalformed // KindBodyLimit indicates the response body exceeded the specified limit. KindBodyLimit // KindInterrupted indicates the operation was cancelled before completion. KindInterrupted )
Kind.String
func (k Kind) String() string
String returns a neutral, human-readable name for the failure kind.
Options
type Options struct {
UserAgent string
// MaxConnections configures connection pooling. When positive, MaxConnsPerHost
// and MaxIdleConnsPerHost are set to n, and MaxIdleConns to 2n.
// When 0, net/http's default pool settings are kept.
MaxConnections int
TLS *tls.Config // cloned per handshake with ServerName set; NextProtos forced to http/1.1
}
Options configures an HTTP Client.
Response
type Response struct {
// contains filtered or unexported fields
}
Response represents an HTTP response. A Response is owned by a single goroutine and must not be accessed concurrently. After Close, Status retains the received status code, while header accessors return absent, Body returns a KindOther error, and Discard is a no-op.
Response.Body
func (r *Response) Body(limit int64, sink func([]byte)) error
Body streams the decoded response body up to limit bytes to the sink function.
Response.Close
func (r *Response) Close()
Close closes the response body and releases pooled resources. Subsequent calls to Close are no-ops.
Response.ContentType
func (r *Response) ContentType() (string, bool)
ContentType returns the Content-Type header value and true if present and non-empty, or empty string and false.
Response.Discard
func (r *Response) Discard(limit int64)
Discard drains up to limit bytes from the response body to allow connection reuse.
Response.Header
func (r *Response) Header(name string) (string, bool)
Header returns the first non-empty value associated with the given key and true, or empty string and false if absent, empty, or closed.
Response.Location
func (r *Response) Location() (string, bool)
Location returns the decoded ISO-8859-1 Location header value and true if present and non-empty, or empty string and false.
Response.RetryAfter
func (r *Response) RetryAfter() (time.Duration, bool)
RetryAfter parses a digits-only Retry-After header as a duration.
Response.Status
func (r *Response) Status() int
Status returns the HTTP status code received.