Guidedog Manual 0.6.3
Idioma
En esta página
Guidedog / Documentación 0.6.3

fetch

Types

fetch.Error :: enum u8
None
Unsupported

not http, https, or a file; or a request send cannot make.

Missing

no such file.

Network

the connection or transfer failed.

Timeout
Status

the server answered with a status other than 2xx.

Too_Large
Redirect

send was answered with a redirect (3xx), which it does not follow.

Cancelled

Options.cancel was set during the transfer.

Memory

the allocator refused a complete result.

fetch.Header :: struct

Header is one HTTP header. A response’s names are in lowercase.

name: string
value: string
fetch.Options :: struct

Options bound one get or send. The zero value is the defaults.

timeout_ms: int

the whole transfer; 0: 30 seconds; below 0: none.

max_bytes: int

the body read; 0: 64 MiB.

user_agent: string
cancel: ^bool

cancel, when set, stops the transfer once the value it points to becomes true; the caller keeps it alive until the call returns.

fetch.Request :: struct

Request is one HTTP request for send. The body is body, or the contents of the file body_path, read as it is sent; at most one of them is given.

method: string

«GET» when «».

url: string

http or https.

headers: []Header
body: []u8
body_path: string
fetch.Response :: struct

Response is what send received, or why it could not. A response with a status other than 2xx has error .Status and keeps its body and headers, which often explain it; a redirect has error .Redirect and its location header. Everything in it is allocated with the allocator given to send, and the caller owns it: destroy_response frees it.

status: int
body: []u8
headers: []Header
error: Error
message: string
fetch.Result :: struct

Result is what get read, or why it could not. Its data, url, and message are allocated with the allocator given to get, and the caller owns them: destroy frees them.

data: []u8
url: string

where the data came from, after redirects.

status: int

the HTTP status, or 0 for files.

error: Error
message: string

what went wrong, for a report.

Procedures

fetch.destroy :: proc(r: ^Result, allocator := context.allocator)

destroy frees what get allocated for r, with the same allocator, and clears r.

fetch.destroy_response :: proc(r: ^Response, allocator := context.allocator)

destroy_response frees what send allocated for r, with the same allocator, and clears r. It is safe on a zero Response.

fetch.get :: proc(location: string, options: Options, allocator := context.allocator) -> Result

get reads a URL or path, allocating the result with allocator; the caller owns it and frees it with destroy. It fails with an Error in the result, never a panic: a file that is missing, a transfer that fails, times out, answers with a status other than 2xx, or grows beyond max_bytes. It blocks until the transfer ends; threads may call it at once.

fetch.header :: proc(r: Response, name: string) -> string

header is the value of the response header name (in lowercase), or «» when r has none.

fetch.send :: proc(r: Request, options: Options, allocator := context.allocator) -> Response

send makes the HTTP request r and returns the response, allocated with allocator; the caller owns it and frees it with destroy_response. It never follows a redirect and never sends a header whose name or value holds a line break (.Unsupported). It fails with an Error in the response, never a panic, and blocks until the transfer ends; threads may call it at once.

Foreign imports

foreign import fetch.curl "system:curl"

Disponibilidad: Darwin, Linux, FreeBSD, OpenBSD, NetBSD, WASI, JS, Orca, Freestanding.

foreign import fetch.winhttp "system:Winhttp.lib"

Disponibilidad: Windows.