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
| Variable | Effect |
|---|---|
KEEL_HTTP_DISABLED=0 | Enable HTTP requests (disabled by default) |
KEEL_HTTP_ALLOWED_HOSTS=a.com,b.com | Restrict to listed domains |
Functions
Request construction
Http.get
String -> Request
Create a GET request for the given URL.
import Http
Http.get "https://example.com"Try itNotes: Returns a Request record. Use Http.send to execute.
Http.post
String -> Request
Create a POST request for the given URL.
import Http
Http.post "https://example.com/api"Try itSee also: Http.get, Http.withBody, Http.withJsonBody
Http.put
String -> Request
Create a PUT request for the given URL.
import Http
Http.put "https://example.com/api/1"Try itSee also: Http.post, Http.patch
Http.patch
String -> Request
Create a PATCH request for the given URL.
import Http
Http.patch "https://example.com/api/1"Try itHttp.delete
String -> Request
Create a DELETE request for the given URL.
import Http
Http.delete "https://example.com/api/1"Try itSee also: Http.get
Http.request
String -> String -> Request
Create a request with a custom HTTP method.
import Http
Http.request "OPTIONS" "https://example.com"Try itNotes: First argument is the HTTP method, second is the URL.
Request modification
Http.withHeader
String -> String -> Request -> Request
Add a header to the request.
import Http
Http.get "https://example.com" |> Http.withHeader "Accept" "application/json"Try itNotes: Request is the last argument for pipe-friendliness.
See also: Http.withHeaders
Http.withHeaders
[(String, String)] -> Request -> Request
Add multiple headers to the request.
import Http
Http.get "https://example.com"
|> Http.withHeaders
[("Accept", "text/html")]Try itSee also: Http.withHeader
Http.withBody
String -> Request -> Request
Set the request body as a string.
import Http
Http.post "https://example.com/api"
|> Http.withBody "data"Try itSee also: Http.withJsonBody
Http.withJsonBody
a -> Request -> Request
Set the request body as JSON-encoded value and add Content-Type header.
import Http
Http.post "https://example.com/api" |> Http.withJsonBody { name = "Alice" }Try itNotes: Automatically sets Content-Type to application/json.
See also: Http.withBody
Http.withTimeout
Int -> Request -> Request
Set the request timeout in milliseconds.
import Http
Http.get "https://example.com"
|> Http.withTimeout 5000Try itNotes: Default timeout is 30000ms (30 seconds).
Http.withQueryParam
String -> String -> Request -> Request
Add a query parameter to the request URL.
import Http
Http.get "https://example.com"
|> Http.withQueryParam "page" "1"Try itExecution
Http.send
Request -> Result Response String
Send the HTTP request and return a Result with the Response or an error.
import Http
Http.get "https://example.com"
|> Http.sendTry itNotes: 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.
import Http
let response =
{ status = 200, body = "{}" }
response
|> Http.jsonBodyTry itNotes: Returns Err if the body is not valid JSON.
See also: Http.send, Json.parse