HTTP Class
HTTP client class for GET, POST, PUT, PATCH, DELETE requests with JSON support and parallel requests.
HTTP Class
All HTTP functions are accessed via the HTTP class:
let response = HTTP.get("https://api.example.com/users")
let response = HTTP.post("https://api.example.com/users", { "name": "Alice" })
let response = HTTP.request("DELETE", "https://api.example.com/users/1")
Basic HTTP Requests
HTTP.get(url, options?)
Perform an HTTP GET request. Returns a Future that resolves to the response body as a string.
let resp = HTTP.get("https://api.example.com/users")
if resp["status"] == 200 {
println(resp["body"])
}
HTTP.post(url, body, options?)
Perform an HTTP POST request with a body.
let resp = HTTP.post(
"https://api.example.com/users",
"name=Alice",
{ "headers": { "Content-Type": "application/x-www-form-urlencoded" } }
)
HTTP.put(url, body, options?)
Perform an HTTP PUT request with a body.
let resp = HTTP.put(
"https://api.example.com/users/1",
{ "name": "Alice Updated" }
)
HTTP.delete(url, options?)
Perform an HTTP DELETE request.
let resp = HTTP.delete("https://api.example.com/users/1")
HTTP.patch(url, body, options?)
Perform an HTTP PATCH request with a body.
let resp = HTTP.patch(
"https://api.example.com/users/1",
{ "email": "[email protected]" }
)
HTTP.head(url, options?)
Perform an HTTP HEAD request.
let resp = HTTP.head("https://api.example.com/users")
println(resp["headers"])
JSON HTTP Methods
HTTP.get_json(url)
GET request with automatic JSON parsing of response body.
let data = HTTP.get_json("https://api.example.com/users/1")
println(data["body"]["name"])
HTTP.post_json(url, data)
POST request with automatic JSON serialization.
let resp = HTTP.post_json(
"https://api.example.com/users",
{ "name": "Alice", "email": "[email protected]" }
)
HTTP.put_json(url, data)
PUT request with automatic JSON serialization.
let resp = HTTP.put_json(
"https://api.example.com/users/1",
{ "name": "Alice Updated", "email": "[email protected]" }
)
HTTP.patch_json(url, data)
PATCH request with automatic JSON serialization.
let resp = HTTP.patch_json(
"https://api.example.com/users/1",
{ "email": "[email protected]" }
)
Generic HTTP Request
HTTP.request(method, url, options?)
Perform any HTTP method (GET, POST, PUT, PATCH, DELETE, etc.).
let resp = HTTP.request("DELETE", "https://api.example.com/users/1")
let resp = HTTP.request("PATCH", url, { "body": json, "headers": headers })
Status Code Helpers
http_ok(resp)
Check if status is exactly 200
http_success(resp)
Check if status is 2xx
http_redirect(resp)
Check if status is 3xx
http_client_error(resp)
Check if status is 4xx
http_server_error(resp)
Check if status is 5xx
JSON Helpers
HTTP.json_parse(string)
Parse a JSON string into a Soli value
HTTP.json_stringify(value)
Convert a Soli value to a JSON string
Parallel Requests
HTTP.get_all(urls)
Execute multiple GET requests in parallel.
let responses = HTTP.get_all([
"https://api.example.com/users",
"https://api.example.com/posts"
])
HTTP.parallel(requests)
Execute multiple requests of different methods in parallel.
let responses = HTTP.parallel([
{ "method": "GET", "url": "https://api.example.com/users" },
{ "method": "POST", "url": "https://api.example.com/logs", "body": "{}" }
])
HTTP Server
Create a lightweight HTTP server without the full MVC framework. For MVC apps, use get/post in config/routes.sl instead.
http_server_get(path, handler_name)
Register a GET route handler.
Parameters
path : String - Route path (e.g., "/users", "/users/:id")handler_name : String - Handler function namedef health(req)
return {"status": 200, "body": "OK"}
end
http_server_get("/health", "health");
http_server_get("/users/:id", "get_user");
http_server_post(path, handler_name)
Register a POST route handler.
def create_user(req)
let name = req["json"]["name"];
return {"status": 201, "body": "Created: " + name};
end
http_server_post("/users", "create_user");
http_server_put(path, handler_name)
Register a PUT route handler.
http_server_put("/users/:id", "update_user");
http_server_delete(path, handler_name)
Register a DELETE route handler.
http_server_delete("/users/:id", "delete_user");
http_server_route(method, path, handler_name)
Register a route for any HTTP method.
http_server_route("PATCH", "/users/:id", "patch_user");
http_server_listen(port)
Start the HTTP server (blocking call).
Parameters
port : Int - Port number to listen on# Define routes
http_server_get("/", "home");
http_server_get("/health", "health");
http_server_post("/api/users", "create_user");
# Start server (blocks)
http_server_listen(3000);
Handler Function Signature
Handler functions receive a request object and should return a response.
def my_handler(req) let id = req["params"]["id"]; # Path parameters
let name = req["query"]["name"]; # Query string
let data = req["json"]["field"]; # JSON body
let token = req["headers"]["Authorization"]; # Headers
return {"status": 200, "body": "Hello"};
return render_json({"message": "Hello"});
return render_text("Plain text");
return redirect("/other-page");
end