httpclient

package
v0.0.8 Latest Latest
Warning

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

Go to latest
Published: Apr 17, 2026 License: Apache-2.0 Imports: 11 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func DelayWithJitter

func DelayWithJitter(baseDelay, jitter time.Duration) time.Duration

DelayWithJitter returns a duration with random jitter applied. The result is between baseDelay and baseDelay + jitter.

func Sleep

func Sleep(baseDelay, jitter time.Duration)

Sleep pauses execution for the base delay plus random jitter.

Types

type Client

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

Client wraps http.Client with Method-specific defaults and helpers.

func New

func New(opts ...Option) *Client

New creates a new HTTP client with the given options.

func (*Client) Do

func (c *Client) Do(req *http.Request) (*Response, error)

Do executes an HTTP request, handling redirects manually.

func (*Client) Get

func (c *Client) Get(ctx context.Context, url string) (*Response, error)

Get performs an HTTP GET request.

func (*Client) GetJSON

func (c *Client) GetJSON(ctx context.Context, url string, dest any) (*Response, error)

GetJSON performs a GET request and unmarshals the JSON response into dest.

func (*Client) GetWithHeaders

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

GetWithHeaders performs an HTTP GET request with custom headers.

func (*Client) Head

func (c *Client) Head(ctx context.Context, url string) (*Response, error)

Head performs an HTTP HEAD request.

func (*Client) HeadWithHeaders

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

HeadWithHeaders performs an HTTP HEAD request with custom headers.

func (*Client) IsAlive

func (c *Client) IsAlive(ctx context.Context, url string) bool

IsAlive checks if a URL responds with a non-404/502 status code.

func (*Client) Post

func (c *Client) Post(ctx context.Context, url string, body any) (*Response, error)

Post performs an HTTP POST request with a JSON body.

func (*Client) PostForm

func (c *Client) PostForm(ctx context.Context, requestURL string, values url.Values) (*Response, error)

PostForm performs an HTTP POST request with form-encoded body.

func (*Client) PostFormWithHeaders

func (c *Client) PostFormWithHeaders(ctx context.Context, requestURL string, values url.Values, headers map[string]string) (*Response, error)

PostFormWithHeaders performs an HTTP POST request with form-encoded body and custom headers.

func (*Client) PostJSON

func (c *Client) PostJSON(ctx context.Context, url string, body any, dest any) (*Response, error)

PostJSON performs a POST request with a JSON body and unmarshals the response into dest.

func (*Client) PostWithHeaders

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

PostWithHeaders performs an HTTP POST request with a JSON body and custom headers.

func (*Client) Request

func (c *Client) Request(ctx context.Context, method, requestURL string, body io.Reader, headers map[string]string) (*Response, error)

Request performs an HTTP request with an arbitrary method, body, and headers.

type Option

type Option func(*Options)

Option is a functional option for configuring the HTTP client.

func WithBlockCrossDomainRedirects

func WithBlockCrossDomainRedirects() Option

WithBlockCrossDomainRedirects blocks redirects that change the host.

func WithDefaultHeaders

func WithDefaultHeaders(headers map[string]string) Option

WithDefaultHeaders sets headers that are applied to every request.

func WithMaxRedirects

func WithMaxRedirects(n int) Option

WithMaxRedirects sets the maximum number of redirects to follow. Set to 0 to disable redirects. Default is 10.

func WithRedirectTracking

func WithRedirectTracking() Option

WithRedirectTracking enables tracking of the full redirect chain in responses.

func WithTLSVerify

func WithTLSVerify(verify bool) Option

WithTLSVerify controls whether TLS certificate verification is enabled.

func WithTimeout

func WithTimeout(d time.Duration) Option

WithTimeout sets the HTTP request timeout.

type Options

type Options struct {
	Timeout                   time.Duration
	VerifyTLS                 bool
	MaxRedirects              int
	TrackRedirects            bool
	BlockCrossDomainRedirects bool
	DefaultHeaders            map[string]string
}

Options configures the HTTP client behavior.

type RedirectHop

type RedirectHop struct {
	URL        string
	StatusCode int
}

RedirectHop records a single redirect in the chain.

type Response

type Response struct {
	StatusCode    int
	Headers       http.Header
	Body          []byte
	RedirectChain []RedirectHop
}

Response wraps an HTTP response with additional metadata.

Jump to

Keyboard shortcuts

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