Help us shape the future of Go! Take the 2026 Go Developer Survey

httpclient

package
v0.0.0-...-a7c838e Latest Latest
Warning

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

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

Documentation

Overview

Package httpclient wraps net/http.Client with a small, context-first API and composable middleware. It is intentionally thin: the goal is to reduce per-call boilerplate (base URL joining, auth headers, JSON body handling) without hiding net/http semantics.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func DecodeJSON

func DecodeJSON[T any](r *http.Response) (T, error)

DecodeJSON decodes r.Body into a fresh T and closes the body. Use as:

resp, err := client.Get(ctx, "/users/1")
if err != nil { return err }
u, err := httpclient.DecodeJSON[User](resp)

func JSONBody

func JSONBody(v any) *bytes.Reader

JSONBody marshals v and returns a reader suitable for the body parameter of Post/Put/Patch. Panics if v cannot be marshalled; marshal failures here typically indicate a programming bug, not a runtime condition.

Types

type Client

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

Client is an HTTP client with a base URL, a middleware chain, and shortcut methods for common verbs. It is safe for concurrent use once configured.

func New

func New(httpClient *http.Client, baseURL string, mws ...Middleware) *Client

New constructs a Client dispatching via the given http.Client (nil means http.DefaultClient) with baseURL prepended to relative request paths. Middlewares are applied in the order given; the first runs closest to the caller (outermost wrapper).

func (*Client) Delete

func (c *Client) Delete(ctx context.Context, path string, mws ...Middleware) (*http.Response, error)

Delete issues a DELETE request.

func (*Client) Do

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

Do sends req through the client's middleware chain, wrapped on the outside by any per-call middleware. Per-call middleware registered first runs closest to the caller, and all per-call middleware run before the chain configured on the client.

func (*Client) Get

func (c *Client) Get(ctx context.Context, path string, mws ...Middleware) (*http.Response, error)

Get issues a GET request to path (joined with the base URL if relative). Per-call middleware follows the same ordering rules as Client.Do.

func (*Client) Patch

func (c *Client) Patch(ctx context.Context, path string, body io.Reader, mws ...Middleware) (*http.Response, error)

Patch issues a PATCH request with body.

func (*Client) Post

func (c *Client) Post(ctx context.Context, path string, body io.Reader, mws ...Middleware) (*http.Response, error)

Post issues a POST request with body.

func (*Client) Put

func (c *Client) Put(ctx context.Context, path string, body io.Reader, mws ...Middleware) (*http.Response, error)

Put issues a PUT request with body.

type Doer

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

Doer is the minimal interface needed to dispatch an HTTP request. Both *http.Client and middleware wrappers satisfy it.

type DoerFunc

type DoerFunc func(*http.Request) (*http.Response, error)

DoerFunc lets a plain function satisfy Doer.

func (DoerFunc) Do

func (f DoerFunc) Do(req *http.Request) (*http.Response, error)

Do implements Doer.

type Middleware

type Middleware func(Doer) Doer

Middleware wraps a Doer to inject behavior (auth, logging, retries, ...) on every request passing through it. Middlewares compose by wrapping: the first registered runs closest to the outer caller.

func BearerAuth

func BearerAuth(token func() string) Middleware

BearerAuth returns a Middleware that sets `Authorization: Bearer <token>` on every outgoing request. Token is resolved per-request via the supplied function, so rotation is handled by returning a fresh value; empty strings are skipped so anonymous requests pass through.

func Header(key, value string) Middleware

Header returns a Middleware that sets a static header on every request. Existing values for the same key are replaced.

func JSONContent

func JSONContent() Middleware

JSONContent returns a Middleware that sets `Content-Type: application/json` on requests that have a body and do not already specify a content type.

Jump to

Keyboard shortcuts

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