httpx

package
v0.5.0 Latest Latest
Warning

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

Go to latest
Published: Mar 30, 2026 License: EUPL-1.2 Imports: 7 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.

Types

type Client

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

Client wraps HTTP operations with configurable timeout and convenience methods.

func NewClient

func NewClient(opts ...Option) *Client

NewClient creates a new HTTP client with options. Default: *http.Client with 30s timeout.

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 timeout when no deadline exists, 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 WithTimeout

func WithTimeout(timeout time.Duration) Option

WithTimeout sets the default timeout for requests.

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