Skip to content

Std.Http

error InvalidUrl(url: String)

The URL cannot be read as a URL.

  • url: String
error ConnectionFailed(url: String, message: String)

No response was received (name resolution, connection, TLS, too many redirects). message says why.

  • url: String
  • message: String
error ResponseNotText(url: String)

The response body is not valid UTF-8.

  • url: String
effect Http

Sends requests. 4xx and 5xx are responses, not failures; fail only when no response is received.

  • suspend fn send(request: Request) -> Response fails InvalidUrl | ConnectionFailed | ResponseNotText

    Sends request and returns its response.

handler SystemHttp: Http
uses { 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]).

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]
handler TestHttp: Http
uses { 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.

type Incoming

The request as the host handed it over (JS: a Fetch API Request). Used only when writing an export by hand.

type Outgoing

The 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.

fn request(method: Method, url: String) -> Request

A request with no headers and an empty body.

fn add_header(req: Request, name: String, value: String) -> Request

Adds a header after the ones already in req.

fn header(res: Response, name: String) -> Option[String]

The first value of the header name, compared without case.

fn headers(res: Response, name: String) -> List[String]

All values of the header name in order, compared without case.

fn encode_query(pairs: List[(String, String)]) -> String

The query string of pairs, each part percent-encoded (q=%E6%9D%B1&page=2).

fn require_ok(res: Response) -> Response fails UnexpectedStatus

res if its status is 2xx, otherwise fails with UnexpectedStatus.

fn response(status: Int, body: String) -> Response

A 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.