@http
Import this module with import @http.
HTTP client for making requests. Currently supports HTTP only.
Request Functions
Section titled “Request Functions”| Function | Signature | Description |
|---|---|---|
get |
(url string, headers map[string:string] = {:}) -> (HttpResponse, Error) |
GET request — always use destructuring |
post |
(url string, body string, headers map[string:string] = {:}) -> (HttpResponse, Error) |
POST request — always use destructuring |
put |
(url string, body string, headers map[string:string] = {:}) -> (HttpResponse, Error) |
PUT request — always use destructuring |
patch |
(url string, body string, headers map[string:string] = {:}) -> (HttpResponse, Error) |
PATCH request — always use destructuring |
delete |
(url string, headers map[string:string] = {:}) -> (HttpResponse, Error) |
DELETE request — always use destructuring |
head |
(url string, headers map[string:string] = {:}) -> (HttpResponse, Error) |
HEAD request — always use destructuring |
get, post, put, patch, delete, and head are fallible: destructure the result (mut resp, err = ... or mut resp, _ = ...).
A response larger than 1 MB (1,048,576 bytes, headers included) returns an Error with code OutOfRange and no truncated body. A Transfer-Encoding: chunked body is decoded: resp.body is the concatenated chunk data, and a malformed chunk returns an Error with code ParseFailure.
A request with a body sends Content-Type: application/x-www-form-urlencoded only when the caller’s headers name no Content-Type (names are compared case-insensitively). Host, Content-Length, and Connection are always set by the client; a caller entry for any of them is not sent.
A failed request returns an Error with a fixed message and a specific code: Unsupported for an https:// URL or an IPv6 address literal, InvalidInput for a malformed URL, the system’s code for a failed connection (for example ConnectionRefused), ParseFailure for a response that is not HTTP, and Timeout when the 10 second read timeout elapses.
HttpResponse Type
Section titled “HttpResponse Type”HttpResponse is one type shared by @http and @server: it is available when either is imported, and HttpResponse, http.HttpResponse and server.HttpResponse all name it. A server handler can return a response from http.get unchanged.
| Field | Type | Description |
|---|---|---|
status |
i64 |
HTTP status code |
body |
string |
Response body |
headers |
map[string:string] |
Response headers; names are canonicalized (the first letter of each hyphen-separated word is uppercase, the rest lowercase), so the content type is headers["Content-Type"] whatever case the server sent |