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.setHeader "Accept" "application/json"
|> Http.setTimeout 5000
|> Http.send
-- POST with JSON body
let result = Http.post "https://api.example.com/users"
|> Http.setJsonBody { 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.setBody, Http.setJsonBody
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.setHeader
String -> String -> Request -> Request
Add a header to the request.
import Http
Http.get "https://example.com" |> Http.setHeader "Accept" "application/json"Try itNotes: Request is the last argument for pipe-friendliness.
See also: Http.setHeaders
Http.setHeaders
[(String, String)] -> Request -> Request
Add multiple headers to the request.
import Http
Http.get "https://example.com"
|> Http.setHeaders [("Accept", "text/html"), ("X-Custom", "value")]Try itSee also: Http.setHeader
Http.setBody
String -> Request -> Request
Set the request body as a string.
import Http
Http.post "https://example.com/api"
|> Http.setBody "data"Try itSee also: Http.setJsonBody
Http.setJsonBody
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.setJsonBody { name = "Alice" }Try itNotes: Automatically sets Content-Type to application/json.
See also: Http.setBody
Http.setTimeout
Int -> Request -> Request
Set the request timeout in milliseconds.
import Http
Http.get "https://example.com"
|> Http.setTimeout 5000Try itNotes: Default timeout is 30000ms (30 seconds).
Http.setQueryParam
String -> String -> Request -> Request
Add a query parameter to the request URL.
import Http
Http.get "https://example.com"
|> Http.setQueryParam "page" "1"Try itExecution
Http.send
Request -> Result Response HttpError
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.responseJson
Http.responseJson
Response -> Result a HttpError
Parse the response body as JSON.
import Http
let response =
{ status = 200, body = "{}" }
response
|> Http.responseJsonTry itNotes: Returns Err if the body is not valid JSON.
See also: Http.send, Http.responseText, Json.parse
Other
Http.responseText
Response -> String
Extract the response body as a raw string.
import Http
Http.get "https://example.com"
|> Http.send
|> Result.map Http.responseTextTry itNotes: Returns the raw body string without any parsing. Use Http.responseJson to parse as JSON.
See also: Http.responseJson, Http.send