A minimal, zero-dependency, WASM-compatible HTTP client for Go.
- Tiny API:
Get,Post,Put,Delete - Declarative Headers:
ContentTypeJSON(),ContentTypeBinary(), etc. - WASM Compatible: Works in browsers using
fetchAPI and in standard Go - Async Support:
Send(callback) andDispatch(fire-and-forget)
go get github.com/tinywasm/fetchpackage main
import (
"github.com/tinywasm/fetch"
)
func main() {
// Simple GET request using relative path (auto-detects origin in WASM)
fetch.Get("/data").
Send(func(resp *fetch.Response, err error) {
if err != nil {
println("Error:", err.Error())
return
}
println("Status:", resp.Status)
println("Body:", resp.Text())
})
// POST request with JSON body using custom BaseURL
body := []byte(`{"name":"Alice"}`)
fetch.Post("/users").
BaseURL("https://api.example.com").
ContentTypeJSON().
Body(body).
Timeout(5000).
Send(func(resp *fetch.Response, err error) {
if err != nil {
return
}
println("Created:", resp.Text())
})
}- Base URL & Endpoint Resolution - How URLs are resolved automatically
- HTTP Headers - How to set and retrieve headers
- CORS Troubleshooting - Common issues in WASM environments
The following declarative methods are available on Request:
ContentTypeJSON()->application/jsonContentTypeBinary()->application/octet-streamContentTypeForm()->application/x-www-form-urlencodedContentTypeText()->text/plainContentTypeHTML()->text/html
For fire-and-forget requests or centralized error handling:
func main() {
// Set a global handler
fetch.SetHandler(func(resp *fetch.Response) {
if resp.Status >= 400 {
println("Request to", resp.RequestURL, "failed with", resp.Status)
}
})
// Fire and forget
fetch.Post("/analytics").
ContentTypeText().
Body([]byte("event=click")).
Dispatch()
}