Http

Method

Represents an HTTP method.

Header

Represents an HTTP header e.g. Content-Type: application/json

Request

Represents an HTTP request.

Response

Represents an HTTP response.

default_request : Request

A default Request value.

# GET "roc-lang.org"
{ Http.default_request &
    url: "https://www.roc-lang.org",
}

header : ( Str, Str ) -> Header

An HTTP header for configuring requests.

See common headers here.

send! : Request => Result Response [ HttpErr [ Timeout, NetworkError, BadBody, Other (List U8) ] ]

Send an HTTP request, succeeds with a value of Str or fails with an [Err].

# Prints out the HTML of the Roc-lang website.
response = ||
    Http.send!({ Http.default_request & url: "https://www.roc-lang.org" })?


Str.from_utf8(response.body) ?? "Invalid UTF-8"
|> Stdout.line

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 ]