axios for Go
An ergonomic, axios-style HTTP client for Go.
A from-scratch, standard-library-only Go HTTP client that brings the ergonomics of JavaScript's axios to net/http. Create a configured client with axios.New(Config{...}) — BaseURL, default Headers and query Params, a Timeout, Basic or Bearer auth, retries, and request/response interceptors — or reach for the package-level Get/Post helpers backed by a default client. Verb methods encode request bodies automatically by their dynamic type (JSON for structs and maps, form-urlencoded for url.Values, raw for []byte/string/io.Reader) and return a rich Response with JSON, Text, Bytes, OK and Header helpers. Non-2xx statuses resolve to a typed *Error that still carries the parsed Response, ValidateStatus lets you redefine success, and the generic GetJSON[T] fetches and decodes in a single call. No cgo, no third-party dependencies.
Install
$ go get github.com/malcolmston/axiosQuick start
import "github.com/malcolmston/axios"
client := axios.New(axios.Config{
BaseURL: "https://api.example.com",
Timeout: 5 * time.Second,
BearerToken: "secret-token",
})
resp, _ := client.Get("/users/1", &axios.RequestConfig{
Params: url.Values{"expand": {"profile"}},
})
var u User
_ = resp.JSON(&u)
// One-liner fetch + decode with generics.
u2, _ := axios.GetJSON[User](client, "/users/2")
client.Post("/users", User{Name: "Ada", Age: 36})Features
- Configurable client via
NewandConfig(BaseURL,Headers,Params,Timeout,Context) plus package-levelGet/Post/… helpers backed byDefault/SetDefault - Full set of verb methods —
Get,Delete,Head,Options,Post,Put,Patch— all built on the low-levelRequest - Automatic body encoding by dynamic type via
EncodeBody: JSON for structs/maps, form forurl.Values, raw for[]byte/string/io.Reader - Rich
ResponsewithJSON,Text,Bytes,OKandHeaderhelpers over a fully-buffered body - Authentication built in —
BearerTokenandBasicAuth, overridable per request throughRequestConfig - Ordered request & response interceptors —
RequestInterceptorandResponseInterceptor— that mutate or transform in place - Configurable retries via
RetryConfigwithDefaultBackoff(exponential) andDefaultRetryOn(transport errors + 5xx) - Typed
*Errorthat carries the parsedResponseon rejected statuses, unwraps transport errors, and exposesStatusCode - Redefine success with
ValidateStatus(per client or per request), just like axiosvalidateStatus - Generic one-liner fetch + decode —
GetJSON[T]andGetJSONDefault[T] - Zero dependencies — pure Go standard library, nothing to audit but the toolchain