Skip to content
heff

browser.http

For a page’s HTTP requests: a request sent by the browser’s fetch, its body and its response’s bytes, and the response, or why none came. JavaScript’s alone, a page’s; a C program’s is std.http, whose shapes these are (send, get, text), but for what curl has and fetch does not. Import it by a name, import browser.http as HTTP. (browser.dom’s fetchText is a GET of text; browser.remote is an API’s client over this.)

A response is whatever the server answered, a 404 or a 500 too; an error is that none came — the network, a request refused by the browser (another origin’s, without CORS), the time up — with what the browser says of it.

Request

type Request =
{ url : String
, method : String = "GET"
, headers : List (String, String) = []
, body : Bytes = Bytes.empty
, timeout : Int = 0
}

A request: where it goes (relative to the page, as fetch has it), its method, its headers (a name and a value each), its body (sent where it is not empty: a GET or a HEAD has none), and how long it may take in all in milliseconds (0: as long as it takes).

Response

type Response = { status : Int, headers : List (String, String), body : Bytes }

A response: its status, its headers (a name and a value each, the names in lower case, as fetch gives them), and its body.

HttpError

type HttpError = { message : String, url : String }

Why no response came, as the browser says it, and the URL the request was for (as it was given, relative to the page or not).

HttpF

data HttpF (m : Type -> Type) x = Send Request (Either HttpError Response -> x)

http

http : HFunctor

For HTTP requests sent, and their responses, by the browser’s fetch.

send

send : {e : HFunctor}
-> {{ev : http <: e}}
-> Request
-> Co e (Either HttpError Response)

The request sent: its response, or why none came.

get

get : {e : HFunctor}
-> {{ev : http <: e}}
-> String
-> Co e (Either HttpError Response)

A GET of the URL.

text

text : Response -> String

The body as text: UTF-8, what is not UTF-8 U+FFFD.

>>> text { status = 200, headers = Nil, body = Bytes.fromList [104, 105, 255] }
"hi\u{FFFD}"

instanceShowHttpError

instance instanceShowHttpError : Show HttpError

As the browser says it, the URL first.

>>> show ({ message = "Failed to fetch", url = "/api" } : HttpError)
"/api: fetch: Failed to fetch"