Esc
Start typing to search...

Http Module

HTTP client for making network requests.

The Http module uses a request-as-data pattern: functions like Http.get return a Request record (pure data), and only Http.send performs the side effect. Requests are composable via the pipe operator |>.

Common patterns

import Http

-- GET request with headers
let response = Http.get "https://api.example.com/users"
    |> Http.withHeader "Accept" "application/json"
    |> Http.withTimeout 5000
    |> Http.send

-- POST with JSON body
let result = Http.post "https://api.example.com/users"
    |> Http.withJsonBody { name = "Alice" }
    |> Http.send

Security

VariableEffect
KEEL_HTTP_DISABLED=0Enable HTTP requests (disabled by default)
KEEL_HTTP_ALLOWED_HOSTS=a.com,b.comRestrict to listed domains

Functions

Request construction

Http.get

String -> Request

Create a GET request for the given URL.

Example:
import Http

Http.get "https://example.com"
Try it

Notes: Returns a Request record. Use Http.send to execute.

See also: Http.post, Http.send

Http.post

String -> Request

Create a POST request for the given URL.

Example:
import Http

Http.post "https://example.com/api"
Try it

See also: Http.get, Http.withBody, Http.withJsonBody

Http.put

String -> Request

Create a PUT request for the given URL.

Example:
import Http

Http.put "https://example.com/api/1"
Try it

See also: Http.post, Http.patch

Http.patch

String -> Request

Create a PATCH request for the given URL.

Example:
import Http

Http.patch "https://example.com/api/1"
Try it

See also: Http.put, Http.post

Http.delete

String -> Request

Create a DELETE request for the given URL.

Example:
import Http

Http.delete "https://example.com/api/1"
Try it

See also: Http.get

Http.request

String -> String -> Request

Create a request with a custom HTTP method.

Example:
import Http

Http.request "OPTIONS" "https://example.com"
Try it

Notes: First argument is the HTTP method, second is the URL.

See also: Http.get, Http.post

Request modification

Http.withHeader

String -> String -> Request -> Request

Add a header to the request.

Example:
import Http
Http.get "https://example.com" |> Http.withHeader "Accept" "application/json"
Try it

Notes: Request is the last argument for pipe-friendliness.

See also: Http.withHeaders

Http.withHeaders

[(String, String)] -> Request -> Request

Add multiple headers to the request.

Example:
import Http

Http.get "https://example.com"
    |> Http.withHeaders

[("Accept", "text/html")]
Try it

See also: Http.withHeader

Http.withBody

String -> Request -> Request

Set the request body as a string.

Example:
import Http

Http.post "https://example.com/api"
    |> Http.withBody "data"
Try it

See also: Http.withJsonBody

Http.withJsonBody

a -> Request -> Request

Set the request body as JSON-encoded value and add Content-Type header.

Example:
import Http
Http.post "https://example.com/api" |> Http.withJsonBody { name = "Alice" }
Try it

Notes: Automatically sets Content-Type to application/json.

See also: Http.withBody

Http.withTimeout

Int -> Request -> Request

Set the request timeout in milliseconds.

Example:
import Http

Http.get "https://example.com"
    |> Http.withTimeout 5000
Try it

Notes: Default timeout is 30000ms (30 seconds).

Http.withQueryParam

String -> String -> Request -> Request

Add a query parameter to the request URL.

Example:
import Http

Http.get "https://example.com"
    |> Http.withQueryParam "page" "1"
Try it

Execution

Http.send

Request -> Result Response String

Send the HTTP request and return a Result with the Response or an error.

Example:
import Http

Http.get "https://example.com"
    |> Http.send
Try it

Notes: This is the only function that performs IO. Requires KEEL_HTTP_DISABLED=0.

See also: Http.jsonBody

Http.jsonBody

Response -> Result a String

Parse the response body as JSON.

Example:
import Http

let response =
    { status = 200, body = "{}" }

response
    |> Http.jsonBody
Try it

Notes: Returns Err if the body is not valid JSON.

See also: Http.send, Json.parse