Http
Method
Represents an HTTP method: [OPTIONS, GET, POST, PUT, DELETE, HEAD, TRACE, CONNECT, PATCH, EXTENSION Str]
Header
Represents an HTTP header e.g. Content-Type: application/json
.
Header is a { name : Str, value : Str }
.
Request
Represents an HTTP request. Request is a record:
{ method : Method, headers : List Header, uri : Str, body : List U8, timeout_ms : [TimeoutMilliseconds U64, NoTimeout], }
Response
Represents an HTTP response.
Response is a record with the following fields:
{ status : U16, headers : List Header, body : List U8 }
default_request : Request
A default Request
value with the following values:
{ method: GET headers: [] uri: "" body: [] timeout_ms: NoTimeout }
Example:
# GET "roc-lang.org" { Http.default_request & uri: "https://www.roc-lang.org", }
header :
(
Str,
Str
)
-> Header
An HTTP header for configuring requests.
See common headers here.
Example: header(("Content-Type", "application/json"))
send! :
Request
=> Result Response
[
HttpErr
[
Timeout,
NetworkError,
BadBody,
Other (List U8)
]
]
Send an HTTP request, succeeds with a Response
or fails with a [HttpErr _].
# Prints out the HTML of the Roc-lang website. response : Response response = Http.send!({ Http.default_request & uri: "https://www.roc-lang.org" })? Stdout.line!(Str.from_utf8(response.body))?
get! :
Str,
fmt
=> Result body
[
HttpDecodingFailed,
HttpErr
]
where body implements Decoding, fmt implements DecoderFormatting
Try to perform an HTTP get request and convert (decode) the received bytes into a Roc type. Very useful for working with Json.
import json.Json # On the server side we send `Encode.to_bytes({foo: "Hello Json!"}, Json.utf8)` { foo } = Http.get!("http://localhost:8000", Json.utf8)?
get_utf8! :
Str
=> Result Str
[
BadBody Str,
HttpErr
]
Try to perform an HTTP get request and convert the received bytes (in the body) into a UTF-8 string.
# On the server side we, send `Str.to_utf8("Hello utf8")` hello_str : Str hello_str = Http.get_utf8!("http://localhost:8000")?