Std.Http
InvalidUrl
Section titled “InvalidUrl”error InvalidUrl(url: String)The URL cannot be read as a URL.
url: String
ConnectionFailed
Section titled “ConnectionFailed”error ConnectionFailed(url: String, message: String)No response was received (name resolution, connection, TLS, too many redirects). message says why.
url: Stringmessage: String
ResponseNotText
Section titled “ResponseNotText”error ResponseNotText(url: String)The response body is not valid UTF-8.
url: String
effect HttpSends requests. 4xx and 5xx are responses, not failures; fail only when no response is received.
-
suspend fn send(request: Request) -> Response fails InvalidUrl | ConnectionFailed | ResponseNotTextSends
requestand returns its response.
SystemHttp
Section titled “SystemHttp”handler SystemHttp: Httpuses { Async }The host’s HTTP client (JS fetch). It follows redirects, keeps no cookies and ignores the
proxy environment variables. Interrupting the fiber waiting on send aborts the request
([ext.call]).
TestServer
Section titled “TestServer”type TestServer(respond: fn(Request, Int) -> Option[Response], sent: List[Request])The server that TestHttp sends to. respond gets the request and how many requests were sent
before it (from 0), and returns the response, or None for ConnectionFailed. sent holds the
sent requests in order.
respond: fn(Request, Int) -> Option[Response]sent: List[Request]
TestHttp
Section titled “TestHttp”handler TestHttp: Httpuses { State[TestServer] }The test handler. The server is State[TestServer], so use it inside State.run:
server = TestServer(respond: |req, n| Some(Response.new(200, "ok")), sent: [])(res, after) = State.run(server, || with TestHttp { Http.get("https://example.com") })send adds the request to sent and resumes at once. It never fails with InvalidUrl or
ResponseNotText.
Incoming
Section titled “Incoming”type IncomingThe request as the host handed it over (JS: a Fetch API Request). Used only when writing an export by hand.
Outgoing
Section titled “Outgoing”type OutgoingThe response for the host to send (JS: a Fetch API Response).
fn get(url: String) -> Response fails InvalidUrl | ConnectionFailed | ResponseNotText uses { Http }Sends a GET request to url.
fn post(url: String, body: String) -> Response fails InvalidUrl | ConnectionFailed | ResponseNotText uses { Http }Sends a POST request to url with body.
request
Section titled “request”fn request(method: Method, url: String) -> RequestA request with no headers and an empty body.
add_header
Section titled “add_header”fn add_header(req: Request, name: String, value: String) -> RequestAdds a header after the ones already in req.
header
Section titled “header”fn header(res: Response, name: String) -> Option[String]The first value of the header name, compared without case.
headers
Section titled “headers”fn headers(res: Response, name: String) -> List[String]All values of the header name in order, compared without case.
encode_query
Section titled “encode_query”fn encode_query(pairs: List[(String, String)]) -> StringThe query string of pairs, each part percent-encoded (q=%E6%9D%B1&page=2).
require_ok
Section titled “require_ok”fn require_ok(res: Response) -> Response fails UnexpectedStatusres if its status is 2xx, otherwise fails with UnexpectedStatus.
response
Section titled “response”fn response(status: Int, body: String) -> ResponseA response with no headers.
fn serve[E, F](app: fn(Request) -> Response fails E uses { Async, ProcessHost, Spawn[F, Async] }, incoming: Incoming) -> Outgoing uses { Async, ProcessHost }The entry of an HTTP service ([lib.http-service]): @entry(Http.serve) on app makes the
host call app once for each request, and each call is one launch. The request is read whole
before app runs. A method not in Method is answered with 405, and a body that is not UTF-8
or a path or query with a malformed % with 400, without calling app. When app fails, has
a defect or returns a response the host cannot send, the cause is written to standard error as
one line and the response is 500; the cause is never in the response. An HTTP service runs in
a process, so app is given ProcessHost ([effect.host]) and can write with Process { ... }.
app is called inside a Task.Background that provides Spawn by name. The response goes to
the host as soon as app returns, and the launch ends when the release of that background has
waited for the tasks app started. A task that ends with an error or a defect writes its cause
to standard error as one line.