http
import httpFunctions
Section titled “Functions”| Function | Signature | Description |
|---|---|---|
serve | (Int, Fn[Unknown] -> Unknown) -> Unit | Start an HTTP server on the given port with a request handler |
response | (Int, String) -> Response | Create a plain text HTTP response with status code |
json | (Int, String) -> Response | Create a JSON HTTP response with status code |
with_headers | (Int, String, Map[String, String]) -> Response | Create a response with custom headers |
redirect | (String) -> Response | Create a 302 temporary redirect response |
status | (Response, Int) -> Response | Set the status code on a response |
body | (Response) -> String | Get the body string from a response |
set_header | (Response, String, String) -> Response | Set a header on a response |
get_header | (Response, String) -> Option[String] | Get a header value from a response |
req_method | (Request) -> String | Get the HTTP method of a request (GET, POST, etc.) |
req_path | (Request) -> String | Get the URL path of a request |
req_body | (Request) -> String | Get the body string of a request |
req_header | (Request, String) -> Option[String] | Get a header value from a request |
query_params | (Request) -> Map[String, String] | Get all query parameters from a request as a map |
get | (String) -> Result[String, String] | Send an HTTP GET request and return the response body |
post | (String, String) -> Result[String, String] | Send an HTTP POST request with a body string |
put | (String, String) -> Result[String, String] | Send an HTTP PUT request |
patch | (String, String) -> Result[String, String] | Send an HTTP PATCH request |
delete | (String) -> Result[String, String] | Send an HTTP DELETE request |
request | (String, String, String, Map[String, String]) -> Result[String, String] | Send a custom HTTP request with method, URL, body, and headers |
Reference
Section titled “Reference”http.serve(port: Int, f: Fn[Unknown] -> Unknown) -> Unit
Section titled “http.serve(port: Int, f: Fn[Unknown] -> Unknown) -> Unit”Start an HTTP server on the given port with a request handler
http.serve(3000, (req) => http.response(200, "ok"))http.response(status: Int, body: String) -> Response
Section titled “http.response(status: Int, body: String) -> Response”Create a plain text HTTP response with status code
http.response(200, "Hello!")http.json(status: Int, body: String) -> Response
Section titled “http.json(status: Int, body: String) -> Response”Create a JSON HTTP response with status code
http.json(200, json.stringify(data))http.with_headers(status: Int, body: String, headers: Map[String, String]) -> Response
Section titled “http.with_headers(status: Int, body: String, headers: Map[String, String]) -> Response”Create a response with custom headers
http.with_headers(200, body, {"Content-Type": "text/html"})http.redirect(url: String) -> Response
Section titled “http.redirect(url: String) -> Response”Create a 302 temporary redirect response
http.redirect("/new-path")http.status(resp: Response, code: Int) -> Response
Section titled “http.status(resp: Response, code: Int) -> Response”Set the status code on a response
http.status(resp, 201)http.body(resp: Response) -> String
Section titled “http.body(resp: Response) -> String”Get the body string from a response
let text = http.body(resp)http.set_header(resp: Response, key: String, value: String) -> Response
Section titled “http.set_header(resp: Response, key: String, value: String) -> Response”Set a header on a response
http.set_header(resp, "X-Custom", "value")http.get_header(resp: Response, key: String) -> Option[String]
Section titled “http.get_header(resp: Response, key: String) -> Option[String]”Get a header value from a response
let ct = http.get_header(resp, "Content-Type")http.req_method(req: Request) -> String
Section titled “http.req_method(req: Request) -> String”Get the HTTP method of a request (GET, POST, etc.)
let method = http.req_method(req)http.req_path(req: Request) -> String
Section titled “http.req_path(req: Request) -> String”Get the URL path of a request
let path = http.req_path(req)http.req_body(req: Request) -> String
Section titled “http.req_body(req: Request) -> String”Get the body string of a request
let body = http.req_body(req)http.req_header(req: Request, key: String) -> Option[String]
Section titled “http.req_header(req: Request, key: String) -> Option[String]”Get a header value from a request
let auth = http.req_header(req, "Authorization")http.query_params(req: Request) -> Map[String, String]
Section titled “http.query_params(req: Request) -> Map[String, String]”Get all query parameters from a request as a map
let params = http.query_params(req) // {"page": "1", "q": "test"}http.get(url: String) -> Result[String, String]
Section titled “http.get(url: String) -> Result[String, String]”Send an HTTP GET request and return the response body
let html = http.get("https://example.com")http.post(url: String, body: String) -> Result[String, String]
Section titled “http.post(url: String, body: String) -> Result[String, String]”Send an HTTP POST request with a body string
let resp = http.post("https://api.example.com", body)http.put(url: String, body: String) -> Result[String, String]
Section titled “http.put(url: String, body: String) -> Result[String, String]”Send an HTTP PUT request
let resp = http.put(url, body)http.patch(url: String, body: String) -> Result[String, String]
Section titled “http.patch(url: String, body: String) -> Result[String, String]”Send an HTTP PATCH request
let resp = http.patch(url, body)http.delete(url: String) -> Result[String, String]
Section titled “http.delete(url: String) -> Result[String, String]”Send an HTTP DELETE request
let resp = http.delete(url)http.request(method: String, url: String, body: String, headers: Map[String, String]) -> Result[String, String]
Section titled “http.request(method: String, url: String, body: String, headers: Map[String, String]) -> Result[String, String]”Send a custom HTTP request with method, URL, body, and headers
let resp = http.request("PUT", url, body, headers)