httpx

package
v0.11.10 Latest Latest
Warning

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

Go to latest
Published: Nov 16, 2026 License: EUPL-1.2 Imports: 8 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func ReadBody

func ReadBody(body io.ReadCloser, maxBodyBytes int64) (bodyStr string, truncated bool, err error)

ReadBody reads an HTTP response body with optional size limiting. If maxBodyBytes <= 0, reads the entire body without limit (existing notify behavior). If maxBodyBytes > 0, limits reading to maxBodyBytes and sets truncated=true if exceeded. Uses io.LimitReader to detect truncation by reading maxBodyBytes+1. Tolerates EOF and ErrUnexpectedEOF errors per existing responseToString pattern. Returns the body string and truncation status, or an error on read failure.

func RedactError added in v0.11.7

func RedactError(err error) error

RedactError hides untrusted transport diagnostics (URLs, credentials and response payloads), while preserving the cause for errors.Is and errors.As.

Types

type Client

type Client struct {
	// contains filtered or unexported fields
}

Client wraps HTTP operations with an optional caller-selected timeout and convenience methods.

func NewClient

func NewClient(opts ...Option) *Client

NewClient creates a new HTTP client with options. By default, total request lifetime is controlled by the caller's context. TCP connection establishment is bounded to 30 seconds and TLS negotiation to 10 seconds. These setup limits do not bound response headers or body reading.

func (*Client) Delete

func (c *Client) Delete(ctx context.Context, url string, headers map[string]string, maxBodyBytes int64) (*Response, error)

Delete is a convenience method for DELETE requests.

func (*Client) Do

func (c *Client) Do(ctx context.Context, method, url string, headers map[string]string, body string, maxBodyBytes int64) (*Response, error)

Do executes an HTTP request with the given parameters. Creates a request with context, applies any explicit client timeout, applies headers, executes via doer, reads response via ReadBody. Returns Response or error.

func (*Client) Get

func (c *Client) Get(ctx context.Context, url string, headers map[string]string, maxBodyBytes int64) (*Response, error)

Get is a convenience method for GET requests.

func (*Client) Post

func (c *Client) Post(ctx context.Context, url string, headers map[string]string, body string, maxBodyBytes int64) (*Response, error)

Post is a convenience method for POST requests.

func (*Client) Put

func (c *Client) Put(ctx context.Context, url string, headers map[string]string, body string, maxBodyBytes int64) (*Response, error)

Put is a convenience method for PUT requests.

type HTTPDoer

type HTTPDoer interface {
	Do(req *http.Request) (*http.Response, error)
}

HTTPDoer abstracts HTTP request execution for testing. *http.Client satisfies this interface natively.

type Option

type Option func(*Client)

Option configures a Client.

func WithDoer

func WithDoer(doer HTTPDoer) Option

WithDoer sets a custom HTTPDoer (for testing).

func WithDoerDecorator added in v0.11.7

func WithDoerDecorator(decorate func(HTTPDoer) HTTPDoer) Option

WithDoerDecorator wraps the constructed transport without replacing its timeout policy. The decorator is useful for request instrumentation and must preserve cancellation.

func WithTimeout

func WithTimeout(timeout time.Duration) Option

WithTimeout sets a request timeout when timeout is positive. A shorter parent context deadline remains authoritative through normal context composition.

type Response

type Response struct {
	StatusCode int
	Body       string
	Headers    map[string]string
	Truncated  bool
}

Response represents an HTTP response with parsed components. StatusCode is the HTTP status code (e.g., 200, 404, 503). Body is the response body as a string. Headers maps canonicalized header names to their values (multi-value headers joined with ", "). Truncated indicates whether the response body was truncated due to size limits.

Jump to

Keyboard shortcuts

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