response

package
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Feb 4, 2026 License: MIT Imports: 7 Imported by: 0

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:

Metadata

Response includes useful metadata:

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 New

func New(url string, method string, payload any, opt *options.Option) Response

New initializes a new Response instance with basic details

func (*Response) Buffer

func (r *Response) Buffer() *bytes.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) Bytes

func (r *Response) Bytes() []byte

Bytes returns the response body as a byte slice

func (*Response) Len

func (r *Response) Len() int64

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

func (r *Response) PopulateResponse(resp *http.Response, start time.Time)

PopulateResponse populates the Response struct with data from an http.Response

func (*Response) String

func (r *Response) String() string

String returns the response body as a string

Jump to

Keyboard shortcuts

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