A go-wasm library that wraps the Fetch API
go get github.com/mlctrez/wasm-fetch
Importing net/http adds ~4 MBs to your wasm binary. If that's an issue for you, you can use this library to make fetch calls.
Forked from https://github.com/marwan-at-work/wasm-fetch to allow use in https://github.com/maxence-charriere/go-app. This fork provides implementations for wasm and a stub for !wasm that allows importing and usage into multi-architecture source.
package main
import (
"context"
"time"
"github.com/mlctrez/wasm-fetch"
)
ctx, cancel := context.WithTimeout(context.Background(), time.Second)
defer cancel()
resp, err := fetch.Fetch("/some/api/call", &fetch.Opts{
Body: strings.NewReader(`{"one": "two"}`),
Method: fetch.MethodPost,
Signal: ctx,
})
// use response...
GO-WASM is currently experimental and therefore this package is experimental as well, things can break unexpectedly.