Standard Library
std.http
Blocking HTTP client helpers for tool-style scripts.
Exported functions
Create a request with empty headers/body and no timeout override.
Create a request with explicit headers, body, and timeout.
Examples
use http from "std.http"
const request = http.new_request_with(
"GET", "https://example.com/", {"accept": "text/html"}, "", 5000
)
@println(request.timeout_ms) # 5000Append query parameters to a request URL.
Examples
use http from "std.http"
const request = http.new_request("GET", "https://example.com/search")
const search = http.with_query(request, {"q": "tea"})
@println(search.url) # https://example.com/search?q=teaSend an HTTP request synchronously.
Examples
use http from "std.http"
const request = http.new_request_with(
"GET", "https://example.com/", {"accept": "text/html"}, "", 5000
)
const response = http.check(http.send(request))
@println(response.body)Send a GET request.
Examples
use http from "std.http"
const response = http.get("https://example.com/")
@println(response.status)
if response.ok
@println(response.body)
endSend a GET request with headers.
Send a POST request with a text body.
Send a POST request with headers and a text body.
Send a PUT request with a text body.
Send a PATCH request with a text body.
Send a DELETE request.
Send a JSON POST request.
Examples
use http from "std.http"
# Requires a local API accepting POST /messages.
const response = http.post_json("http://127.0.0.1:8080/messages", {"text": "Hello, Tea!"})
@println(http.check(response).status)Decode a JSON response body.
Examples
use http from "std.http"
# Requires a local API returning a JSON object of strings.
const response = http.check(http.get("http://127.0.0.1:8080/config"))
const config: Dict[String, String] = http.decode_json[Dict[String, String]](response)
@println(config["name"])Return a response header by name when present.
Examples
use http from "std.http"
const response = http.get("https://example.com/")
const content_type = http.header(response, "Content-Type")
@println(content_type ?? "No content type provided")Return a response header by name or a fallback value.
Return the text body of a response.
Return the raw body bytes of a response.
Fail when the response status is not successful.
Examples
use http from "std.http"
const response = http.check(http.get("https://example.com/"))
@println(response.body) # Reached only for a successful responseDownload a URL to a file path and return the response.