DocsOverview

Overview

A small, zero-dependency HTTP client where every request is a struct and every response comes back buffered or streamed.

http is a lightweight client for talking to JSON and streaming HTTP APIs. You build a Client from a Config, then call one method per verb. Every call takes a context and a request struct and hands back a *Response with the status code, body, and headers. The client is pure stdlib with no third-party dependencies, so it adds nothing transitive to your build.

Install

go get github.com/toaweme/http

The package is named http, so import it as plain http. It only clashes with the standard library's net/http in a file that imports both, where you alias it as thttp.

Make a request

Construct a client once against a base URL, then reuse it for every call.

client := http.NewClient(http.Config{
	BaseURL:   "https://api.example.com",
	UserAgent: "demo/1.0.0",
	Platform:  "cli",
})

resp, err := client.Get(ctx, http.GetRequest{
	Request: http.Request{Path: "/users/1"},
})
if err != nil {
	return err
}
fmt.Println(resp.StatusCode, string(resp.Body))

A GetRequest carries a Request, the shared shape that holds the path, query, headers, and request identifiers. The body-carrying verbs (Post, Put, Patch) embed the same Request and add a Body. Send JSON with the JSON helper and decode the reply with the generic FromJSON.

body, _ := http.JSON(map[string]string{"name": "ada"})

resp, err := client.Post(ctx, http.PostRequest{
	Request: http.Request{Path: "/users"},
	Body:    []byte(body),
})
if err != nil {
	return err
}

user, err := http.FromJSON[User](resp.Body)

Note

The client returns a non-nil error only when the request could not be sent or read. An HTTP error status such as 404 or 500 is a normal response, so check resp.StatusCode yourself. See Errors for the full model.

Where to go next

Start with the guides to learn how each part behaves, then reach for the reference when you need the exact signature or header name.