Documentation
¶
Overview ¶
Package response provides the Response type returned by HTTP client operations.
Response contains both the HTTP response data and metadata about the request:
resp, err := client.Get(url)
if err != nil {
log.Fatal(err)
}
fmt.Println(resp.StatusCode) // HTTP status code
fmt.Println(resp.String()) // response body as string
fmt.Println(resp.Bytes()) // response body as []byte
fmt.Println(resp.AccessTime) // request duration
Response Body ¶
The response body is available through several methods:
- Response.String - body as string
- Response.Bytes - body as byte slice
- Response.Buffer - body as *bytes.Buffer
- Response.Len - body length
Metadata ¶
Response includes useful metadata:
- Response.UniqueIdentifier - unique request ID for tracing
- Response.RequestTime - when the request was sent
- Response.ResponseTime - when the response was received
- Response.AccessTime - total request duration
- Response.Redirected - whether a redirect occurred
- Response.Location - final URL after redirects
Error Handling ¶
The Response.Error field stores any error that occurred during the request. This is useful for batch operations where responses are collected for later inspection:
for _, resp := range responses {
if resp.Error != nil {
log.Printf("request to %s failed: %v", resp.URL, resp.Error)
}
}
Index ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type ContentRange ¶
type ContentRange struct {
// Unit is the range unit, typically "bytes"
Unit string
// Start is the starting byte offset of the range
Start int64
// End is the ending byte offset of the range (inclusive)
End int64
// Total is the total size of the resource, or -1 if unknown (indicated by "*")
Total int64
}
ContentRange holds parsed Content-Range header information from partial content responses.
type Response ¶
type Response struct {
UniqueIdentifier string // Unique ID for the request, generated internally
URL string // URL the request was made to
Method string // HTTP method used (e.g., GET, POST)
RequestPayload any // Payload sent with the request
Options *options.Option // Configuration options for the request
RequestTime int64 // Timestamp of when the request was initiated
ResponseTime int64 // Timestamp of when the response was received
ProcessedTime int64 // Duration taken to process the request
Status string // HTTP status message (e.g., "200 OK")
StatusCode int // HTTP status code (e.g., 200, 404)
Proto string // Protocol used (e.g., HTTP/1.1)
Header http.Header // Headers included in the response
ContentLength int64 // Length of the response content
TransferEncoding []string // Transfer encoding details from the response
CompressionType options.CompressionType // Type of compression applied to the response
Uncompressed bool // Indicates if the response was uncompressed
Cookies []*http.Cookie // Cookies received with the response
AccessTime time.Duration // Time taken to complete the request
Body options.WriteCloserBuffer // The response body as a buffer
TLS *tls.ConnectionState // Details about the TLS connection
// Error stores any error encountered during the request. This field exists
// in addition to the error returned by request functions (Get, Post, etc.)
// to support batch operations and response history. When responses are stored
// in a collection for later inspection, the Error field allows iteration
// through responses to determine which requests failed and why, without
// requiring separate error tracking.
//
// For immediate error handling, check the returned error. For deferred
// inspection of stored responses, check this field.
Error error
Redirected bool // Indicates if the request was redirected
Location string // New location if the request was redirected
// Range response fields (RFC 7233)
ContentRange *ContentRange // Parsed Content-Range header for 206 responses
AcceptRanges string // Accept-Ranges header value (e.g., "bytes" or "none")
IsPartialContent bool // True if response is 206 Partial Content
}
Response represents the HTTP response along with additional metadata
func (*Response) Buffer ¶
Buffer returns the response body as a *bytes.Buffer for efficient streaming operations. Returns nil if the body is empty or was not stored (e.g., when writing directly to file).
func (*Response) Len ¶
Len returns the length of the response body If there is no body, it returns -1 to indicate there is an issue
func (*Response) PopulateResponse ¶
PopulateResponse populates the Response struct with data from an http.Response