Documentation
¶
Overview ¶
Package request is a small, dependency-free Go toolkit for working with REST/JSON HTTP APIs. It grows by adding focused, composable helpers rather than shipping a single mega-client.
The current public surface is intentionally small:
- Send — generic JSON in / typed Go out for any HTTP call.
- SendX — the panicking variant of Send, for cases where a non-2xx response is a programmer error or process-fatal condition.
- Options, Auth, BasicAuth, BearerAuth — the configuration types both helpers build on.
A typical call looks like:
result, err := request.Send[MyResponse](request.Options{
Method: http.MethodPost,
Url: "https://api.example.com/things",
Body: MyRequest{Name: "hello"},
Headers: map[string]string{"X-Trace": "abc"},
Auth: request.BearerAuth{Token: token},
})
JSON encoding of the body, JSON decoding of the response, non-2xx status codes, raw []byte / io.Reader payloads, and pluggable auth schemes are all handled by Send. The package has no third-party dependencies and works on Go 1.22+ (requires generics).
When to use this package ¶
Use it when stdlib net/http feels verbose and you want typed JSON responses without a full client (retries, connection pooling, middleware). Send covers "JSON in, typed Go out"; SendX covers the same case in a panic-on-failure form.
When NOT to use this package ¶
If you need retries, timeouts, circuit breakers, tracing, automatic rate-limit handling, or a pre-configured HTTP client, reach for net/http directly or a library such as hashicorp/go-retryablehttp.
Package request provides a small generic helper for sending HTTP requests and decoding JSON responses.
Index ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func Download ¶ added in v0.2.2
Download performs opts and writes the response body to the file path specified by `to`. It returns an error if the request cannot be built or sent, if the server responds with a non-2xx status, or if writing the file fails.
func DownloadX ¶ added in v0.2.2
DownloadX performs the same operation as Download but panics on error.
func Send ¶
Send performs opts and decodes the response body into *T.
It returns an error if the request cannot be built or sent, if the server replies with a non-2xx status, or if the response body cannot be decoded as JSON.
func SendX ¶ added in v0.2.0
SendX is the panicking variant of Send.
It calls Send[T] and panics with the returned error if it is non-nil. It is meant for cases where an HTTP failure is a programmer error or a process-fatal condition (similar to entgo.io/ent's Must helpers, or regexp.MustCompile): the request is so fundamental to the program's flow that handling the error would only obscure control flow.
For most code paths, prefer Send and return the error.
Example:
user := request.SendX[User](request.Options{
Method: http.MethodGet,
Url: "https://api.example.com/users/1",
Auth: request.BearerAuth{Token: "secret"},
})
fmt.Println(user.Name) // no error to check
Types ¶
type Auth ¶ added in v0.1.1
type Auth interface {
// contains filtered or unexported methods
}
Auth is implemented by any value that can apply credentials to an outgoing http.Request (e.g. BasicAuth).
type BearerAuth ¶ added in v0.1.1
type BearerAuth struct {
Token string
}
BearerAuth adds an Authorization: Bearer <token> header to the request.
type Options ¶ added in v0.1.1
type Options struct {
Method string
Url string
Body any
Headers map[string]string
Auth Auth
Client *http.Client
}
Options describes a single HTTP request.
Body can be any value that json.Marshal can handle. If it is nil, an empty request body is sent. If it is already a []byte or io.Reader, it is used as-is (without re-encoding).
Headers is merged on top of the Content-Type that the package sets for JSON bodies, so callers can override it.