fetch

package module
v0.1.19 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Feb 13, 2026 License: MIT Imports: 6 Imported by: 0

README

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

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func GetBaseURL added in v0.1.14

func GetBaseURL() string

GetBaseURL returns the current global base URL.

func SetBaseURL added in v0.1.14

func SetBaseURL(url string)

SetBaseURL sets the global base URL for all requests.

func SetHandler added in v0.1.13

func SetHandler(fn func(*Response))

SetHandler sets the global handler for Dispatch requests.

func SetLog added in v0.1.13

func SetLog(fn func(...any))

SetLog sets the logger function for debugging.

Types

type EndpointProvider added in v0.1.14

type EndpointProvider interface {
	HandlerName() string
}

EndpointProvider allows structs to define their API endpoint

type Header struct {
	Key   string
	Value string
}

Header represents a single HTTP header key-value pair.

type Request added in v0.1.13

type Request struct {
	// contains filtered or unexported fields
}

Request represents an HTTP request builder.

func Delete added in v0.1.13

func Delete(endpoint any) *Request

Delete creates a new DELETE request.

func Get added in v0.1.13

func Get(endpoint any) *Request

Get creates a new GET request.

func Post added in v0.1.13

func Post(endpoint any) *Request

Post creates a new POST request.

func Put added in v0.1.13

func Put(endpoint any) *Request

Put creates a new PUT request.

func (*Request) BaseURL added in v0.1.14

func (r *Request) BaseURL(url string) *Request

BaseURL sets a per-request base URL override.

func (*Request) Body added in v0.1.13

func (r *Request) Body(data []byte) *Request

Body sets the request body.

func (*Request) ContentTypeBinary added in v0.1.13

func (r *Request) ContentTypeBinary() *Request

ContentTypeBinary sets Content-Type to application/octet-stream

func (*Request) ContentTypeForm added in v0.1.13

func (r *Request) ContentTypeForm() *Request

ContentTypeForm sets Content-Type to application/x-www-form-urlencoded

func (*Request) ContentTypeHTML added in v0.1.13

func (r *Request) ContentTypeHTML() *Request

ContentTypeHTML sets Content-Type to text/html

func (*Request) ContentTypeJSON added in v0.1.13

func (r *Request) ContentTypeJSON() *Request

ContentTypeJSON sets Content-Type to application/json

func (*Request) ContentTypeText added in v0.1.13

func (r *Request) ContentTypeText() *Request

ContentTypeText sets Content-Type to text/plain

func (*Request) Dispatch added in v0.1.13

func (r *Request) Dispatch()

Dispatch executes the request and sends the response to the global handler. This is a fire-and-forget method.

func (*Request) Header added in v0.1.13

func (r *Request) Header(key, value string) *Request

Header adds a header to the request.

func (*Request) Send added in v0.1.13

func (r *Request) Send(callback func(*Response, error))

Send executes the request and calls the callback with the response.

func (*Request) Timeout added in v0.1.13

func (r *Request) Timeout(ms int) *Request

Timeout sets the request timeout in milliseconds.

type Response added in v0.1.13

type Response struct {
	Status     int
	Headers    []Header
	RequestURL string
	Method     string
	// contains filtered or unexported fields
}

Response represents an HTTP response.

func (*Response) Body added in v0.1.13

func (r *Response) Body() []byte

Body returns the response body as a byte slice.

func (*Response) GetHeader added in v0.1.13

func (r *Response) GetHeader(key string) string

GetHeader returns the value of the specified header. It is case-insensitive.

func (*Response) Text added in v0.1.13

func (r *Response) Text() string

Text returns the response body as a string.

Directories

Path Synopsis
example
web command

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL