Skip to content
/ fetch Public

A lightweight Go library that unifies HTTP requests across environments. fetchgo works with TinyGo + WebAssembly in the browser and with the standard net/http package on the server, providing a seamless API for client and server communication.

License

Notifications You must be signed in to change notification settings

tinywasm/fetch

Repository files navigation

tinywasm/fetch

Project Badges

A minimal, zero-dependency, WASM-compatible HTTP client for Go.

Features

  • Tiny API: Get, Post, Put, Delete
  • Declarative Headers: ContentTypeJSON(), ContentTypeBinary(), etc.
  • WASM Compatible: Works in browsers using fetch API and in standard Go
  • Async Support: Send (callback) and Dispatch (fire-and-forget)

Installation

go get github.com/tinywasm/fetch

Usage

package 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())
		})
}

Documentation

Content-Type Helpers

The following declarative methods are available on Request:

  • ContentTypeJSON() -> application/json
  • ContentTypeBinary() -> application/octet-stream
  • ContentTypeForm() -> application/x-www-form-urlencoded
  • ContentTypeText() -> text/plain
  • ContentTypeHTML() -> text/html

Global Handler (Dispatch)

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()
}

About

A lightweight Go library that unifies HTTP requests across environments. fetchgo works with TinyGo + WebAssembly in the browser and with the standard net/http package on the server, providing a seamless API for client and server communication.

Resources

License

Stars

Watchers

Forks

Sponsor this project

 

Packages

No packages published

Contributors 2

  •  
  •