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

  • A complete body. content-length is set from its size unless the headers already have one.

  • File(path: String, offset: Int, length: Int)

    length bytes of a file, starting at offset, sent by the kernel without copying them into the BEAM. content-length is set to length unless the headers already have one. Set content-type yourself. A range that runs past the end of the file, or a negative one, fails the request with a 500.

  • Chunked(fn(Sender) -> Nil)

    A body sent in parts. Roadrunner calls the function with a Sender; send each part with send_chunk. The body ends when the function returns. A HEAD request 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.

A pushing body, made with push.response.

pub type PushBody

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)

Sends the parts of a Chunked body.

pub type Sender

A WebSocket session to upgrade to, made with websocket.upgrade.

pub type Upgrade

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 new(status: Int) -> response.Response(Body)

A response with an empty 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.

✨ Search Document