gleam/http/response

Types

A HTTP response.

The body of the request is parameterised. The HTTP server or client you are using will have a particular set of types it supports for the body.

pub type Response(body) {
  Response(
    status: Int,
    headers: List(#(String, String)),
    body: body,
  )
}

Constructors

  • Response(
      status: Int,
      headers: List(#(String, String)),
      body: body,
    )

    Arguments

    headers

    The request headers. The keys must always be lowercase.

Values

pub fn expire_cookie(
  response: Response(body),
  name: String,
  attributes: cookie.Attributes,
) -> Response(body)

Expire a cookie value for a client

Note: The attributes value should be the same as when the response cookie was set.

pub fn get_cookies(
  resp: Response(body),
) -> List(#(String, String))

Fetch the cookies sent in a response.

Badly formed cookies will be discarded.

pub fn get_header(
  response: Response(body),
  key: String,
) -> Result(String, Nil)

Get the value for a given header.

If the response does not have that header then Error(Nil) is returned.

pub fn map(
  response: Response(old_body),
  transform: fn(old_body) -> new_body,
) -> Response(new_body)

Update the body of a response using a given function.

pub fn new(status: Int) -> Response(String)

Construct an empty Response.

The body type of the returned response is String and could be set with a call to set_body.

pub fn prepend_header(
  response: Response(body),
  key: String,
  value: String,
) -> Response(body)

Prepend the header with the given value under the given header key.

Similar to set_header except if the header already exists it prepends another header with the same key.

Header keys are always lowercase in gleam_http. To use any uppercase letter is invalid.

pub fn redirect(uri: String) -> Response(String)

Create a response that redirects to the given uri.

pub fn set_body(
  response: Response(old_body),
  body: new_body,
) -> Response(new_body)

Set the body of the response, overwriting any existing body.

pub fn set_cookie(
  response: Response(body),
  name: String,
  value: String,
  attributes: cookie.Attributes,
) -> Response(body)

Set a cookie value for a client

pub fn set_header(
  response: Response(body),
  key: String,
  value: String,
) -> Response(body)

Set the header with the given value under the given header key.

If the response already has that key, it is replaced.

Header keys are always lowercase in gleam_http. To use any uppercase letter is invalid.

pub fn try_map(
  response: Response(old_body),
  transform: fn(old_body) -> Result(new_body, error),
) -> Result(Response(new_body), error)

Update the body of a response using a given result returning function.

If the given function returns an Ok value the body is set, if it returns an Error value then the error is returned.

Search Document