roadrunner_gleam/response
The response a handler returns: a gleam/http response whose body is
a Body. Build it with gleam/http/response, as in
response.new(200) |> response.set_body(Bytes(...)), or with the
helpers here.
Types
What a response sends after its headers.
pub type Body {
Bytes(bytes_tree.BytesTree)
File(path: String, offset: Int, length: Int)
Chunked(fn(Sender) -> Nil)
WebSocket(Upgrade)
Push(PushBody)
}
Constructors
-
Bytes(bytes_tree.BytesTree)A complete body.
content-lengthis set from its size unless the headers already have one. -
File(path: String, offset: Int, length: Int)lengthbytes of a file, starting atoffset, sent by the kernel without copying them into the BEAM.content-lengthis set tolengthunless the headers already have one. Setcontent-typeyourself. A range that runs past the end of the file, or a negative one, fails the request with a500. -
Chunked(fn(Sender) -> Nil)A body sent in parts. Roadrunner calls the function with a
Sender; send each part withsend_chunk. The body ends when the function returns. AHEADrequest gets the headers only, and the function is not called. -
WebSocket(Upgrade)A WebSocket upgrade, made with
websocket.upgrade. -
Push(PushBody)A body that stays open and pushes data, made with
push.response.
The response a handler returns. gleam/http/response’s functions work on
it, such as set_header, prepend_header, set_cookie and redirect.
pub type Response =
response.Response(Body)
Values
pub fn file(path: String) -> Result(Body, Nil)
A File body with the whole file. Fails when the path is not a regular
file that can be read.
pub fn html(
body: string_tree.StringTree,
) -> response.Response(Body)
A 200 response with a text/html body.
pub fn json(
body: string_tree.StringTree,
) -> response.Response(Body)
A 200 response with an application/json body.
pub fn send_chunk(
sender: Sender,
data: bytes_tree.BytesTree,
) -> Result(Nil, Nil)
Send one part of a Chunked body. Fails when the connection is gone,
after which the function should return. Call it only from the function
roadrunner called: from another process it can wait forever on an
HTTP/2 connection that went away.
pub fn text(body: String) -> response.Response(Body)
A 200 response with a text/plain body.