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 => Response

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.default_request & url: "https://www.roc-lang.org" }
    |> Http.send!

response.body
|> Str.fromUtf8
|> Result.withDefault "Invalid UTF-8"
|> Stdout.line

get! : Str, fmt => Result body [HttpDecodingFailed] 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.toBytes {foo: "Hello Json!"} Json.utf8`
{ foo } = Http.get! "http://localhost:8000" Json.utf8

get_utf8! : Str => Result Str [BadBody Str]