fetch

package module
v0.1.13 Latest Latest
Warning

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

Go to latest
Published: Dec 26, 2025 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
  • WASM Compatible: Works in browsers using fetch API and in standard Go
  • Zero Dependencies: No external libraries required
  • Async Support: Send (callback) and Dispatch (fire-and-forget)

Installation

go get github.com/tinywasm/fetch

Usage

package main

import (
	"fmt"
	"github.com/tinywasm/fetch"
)

func main() {
	// Simple GET request
	fetch.Get("https://api.example.com/data").
		Send(func(resp *fetch.Response, err error) {
			if err != nil {
				fmt.Println("Error:", err)
				return
			}
			fmt.Println("Status:", resp.Status)
			fmt.Println("Body:", resp.Text())
		})

	// POST request with JSON body
	body := []byte(`{"name":"Alice"}`)
	fetch.Post("https://api.example.com/users").
		Header("Content-Type", "application/json").
		Body(body).
		Timeout(5000).
		Send(func(resp *fetch.Response, err error) {
			if err != nil {
				return
			}
			fmt.Println("Created:", resp.Text())
		})
}

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 {
			fmt.Printf("Request to %s failed with %d\n", resp.RequestURL, resp.Status)
		}
	})

	// Fire and forget
	fetch.Post("/analytics").Body([]byte("event=click")).Dispatch()
}

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

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 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(url string) *Request

Delete creates a new DELETE request.

func Get added in v0.1.13

func Get(url string) *Request

Get creates a new GET request.

func Post added in v0.1.13

func Post(url string) *Request

Post creates a new POST request.

func Put added in v0.1.13

func Put(url string) *Request

Put creates a new PUT request.

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