httpclient

package
v0.0.0-20260608 Latest Latest
Warning

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

Go to latest
Published: Jun 8, 2026 License: MIT Imports: 7 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Fake

func Fake(mocks map[string]*Response)

Fake globally enables faking and registers mock responses. URL path pattern -> Mock Response

Types

type Client

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

Client wraps a standard HTTP client and provides fluent request building.

func NewClient

func NewClient() *Client

NewClient creates a new fluent HTTP client.

func (*Client) BaseURL

func (c *Client) BaseURL(url string) *Client

BaseURL sets the base URL for the client.

func (*Client) Delete

func (c *Client) Delete(path string) (*Response, error)

Delete executes a DELETE request.

func (*Client) Get

func (c *Client) Get(path string) (*Response, error)

Get executes a GET request.

func (*Client) Head

func (c *Client) Head(path string) (*Response, error)

Head executes a HEAD request.

func (*Client) Patch

func (c *Client) Patch(path string, data any) (*Response, error)

Patch executes a PATCH request.

func (*Client) Post

func (c *Client) Post(path string, data any) (*Response, error)

Post executes a POST request with JSON payload.

func (*Client) PostForm

func (c *Client) PostForm(path string, data map[string]string) (*Response, error)

PostForm sends a form-urlencoded POST request.

func (*Client) Put

func (c *Client) Put(path string, data any) (*Response, error)

Put executes a PUT request.

func (*Client) Retry

func (c *Client) Retry(times int) *Client

Retry sets the number of retries on failure (5xx).

func (*Client) WithHeader

func (c *Client) WithHeader(key, value string) *Client

WithHeader adds a header to the client.

func (*Client) WithHeaders

func (c *Client) WithHeaders(headers map[string]string) *Client

WithHeaders sets multiple headers at once.

func (*Client) WithQuery

func (c *Client) WithQuery(key, value string) *Client

WithQuery adds query parameters.

func (*Client) WithToken

func (c *Client) WithToken(token string) *Client

WithToken sets a Bearer token.

type Response

type Response struct {
	*http.Response
	// contains filtered or unexported fields
}

Response wraps an HTTP response with many convenient helpers.

func (*Response) Body

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

Body returns the raw response body (cached after first read).

func (*Response) ClientError

func (r *Response) ClientError() bool

ClientError returns true for 4xx responses.

func (*Response) Failed

func (r *Response) Failed() bool

Failed returns true if the response is 4xx or 5xx.

func (*Response) Header

func (r *Response) Header(key string) string

Header returns a specific response header.

func (*Response) JSON

func (r *Response) JSON(target any) error

JSON parses the body as JSON into the target.

func (*Response) Ok

func (r *Response) Ok() bool

Ok returns true if the response status is 2xx.

func (*Response) ServerError

func (r *Response) ServerError() bool

ServerError returns true for 5xx responses.

func (*Response) StatusCode

func (r *Response) StatusCode() int

StatusCode returns the HTTP status code.

func (*Response) Successful

func (r *Response) Successful() bool

Successful is an alias for Ok().

Jump to

Keyboard shortcuts

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