fetch

package
v0.8.0 Latest Latest
Warning

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

Go to latest
Published: Aug 14, 2026 License: MIT Imports: 8 Imported by: 0

Documentation

Overview

Package fetch provides the HTTP API exposed to WebGo programs.

Index

Constants

This section is empty.

Variables

View Source
var ErrBodyConsumed = errors.New("response body has already been consumed")

ErrBodyConsumed reports a second attempt to consume one response body.

Functions

This section is empty.

Types

type API

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

API binds WebGo Fetch calls to one page URL and network executor.

func New

func New(baseURL *url.URL, do func(context.Context, *network.Request) (*network.Response, error)) *API

New creates a page-scoped Fetch API.

func NewPage

func NewPage(ctx context.Context, baseURL *url.URL, do func(context.Context, *network.Request) (*network.Response, error), enqueue func(func()) bool) *API

NewPage creates an asynchronous Fetch API bound to a page callback queue.

func (*API) Close

func (api *API) Close()

Close rejects new Fetches and waits for all request goroutines to release references.

func (*API) Fetch

func (api *API) Fetch(request Request, success func(Response), failure func(string))

Fetch starts request asynchronously and delivers exactly one callback.

type CredentialsMode

type CredentialsMode string

CredentialsMode controls Cookie and HTTP authentication handling.

const (
	CredentialsOmit       CredentialsMode = "omit"
	CredentialsSameOrigin CredentialsMode = "same-origin"
	CredentialsInclude    CredentialsMode = "include"
)
type Header map[string][]string

Header preserves every value associated with an HTTP header name.

type Request

type Request struct {
	Method      string
	URL         string
	Header      Header
	Body        []byte
	Text        string
	Credentials CredentialsMode
}

Request describes an HTTP request issued by a WebGo program. Body takes precedence over Text when both are set.

type Response

type Response struct {
	Status     int
	StatusText string
	URL        string
	Redirected bool
	Header     Header
	// contains filtered or unexported fields
}

Response is delivered to a successful Fetch callback. Additional response metadata and body helpers are added by the HTTP lifecycle implementation.

func (Response) Bytes

func (response Response) Bytes() ([]byte, error)

Bytes consumes the response body and returns a defensive copy.

func (Response) JSON

func (response Response) JSON(target any) error

JSON consumes and decodes the response body into target.

func (Response) Text

func (response Response) Text() (string, error)

Text consumes the response body as UTF-8 text.

Jump to

Keyboard shortcuts

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