axios for Go

github.com/malcolmston/axios

An ergonomic, axios-style HTTP client for Go.

 GitHubports axios/axios

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

shell
$ go get github.com/malcolmston/axios

Quick start

main.go
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 New and Config (BaseURL, Headers, Params, Timeout, Context) plus package-level Get/Post/… helpers backed by Default/SetDefault
  • Full set of verb methods — Get, Delete, Head, Options, Post, Put, Patch — all built on the low-level Request
  • Automatic body encoding by dynamic type via EncodeBody: JSON for structs/maps, form for url.Values, raw for []byte/string/io.Reader
  • Rich Response with JSON, Text, Bytes, OK and Header helpers over a fully-buffered body
  • Authentication built in — BearerToken and BasicAuth, overridable per request through RequestConfig
  • Ordered request & response interceptors — RequestInterceptor and ResponseInterceptor — that mutate or transform in place
  • Configurable retries via RetryConfig with DefaultBackoff (exponential) and DefaultRetryOn (transport errors + 5xx)
  • Typed *Error that carries the parsed Response on rejected statuses, unwraps transport errors, and exposes StatusCode
  • Redefine success with ValidateStatus (per client or per request), just like axios validateStatus
  • Generic one-liner fetch + decode — GetJSON[T] and GetJSONDefault[T]
  • Zero dependencies — pure Go standard library, nothing to audit but the toolchain
axios