gleam/http/request

Types

pub type Request(body) {
  Request(
    method: Method,
    headers: List(Header),
    body: body,
    scheme: Scheme,
    host: String,
    port: Option(Int),
    path: String,
    query: Option(String),
  )
}

Constructors

  • Request(
      method: Method,
      headers: List(Header),
      body: body,
      scheme: Scheme,
      host: String,
      port: Option(Int),
      path: String,
      query: Option(String),
    )

Functions

pub fn from_uri(uri: Uri) -> Result(Request(String), Nil)

Construct a request from a URI.

pub fn get_cookies(req: Request(a)) -> List(#(String, String))

Fetch the cookies sent in a request.

Note badly formed cookie pairs will be ignored. RFC6265 specifies that invalid cookie names/attributes should be ignored.

pub fn get_header(request: Request(a), key: String) -> Result(
  String,
  Nil,
)

Get the value for a given header.

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

pub fn get_query(request: Request(a)) -> Result(
  List(#(String, String)),
  Nil,
)

Decode the query of a request.

pub fn map(request: Request(a), transform: fn(a) -> b) -> Request(
  b,
)

Update the body of a request using a given function.

pub fn new() -> Request(String)

A request with commonly used default values. This request can be used as an initial value and then update to create the desired request.

pub fn path_segments(request: Request(a)) -> List(String)

Return the non-empty segments of a request path.

pub fn prepend_header(request: Request(a), key: String, value: String) -> Request(
  a,
)
pub fn set_body(req: Request(a), body: b) -> Request(b)

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

pub fn set_cookie(req: Request(a), name: String, value: String) -> Request(
  a,
)

Send a cookie with a request

Multiple cookies are added to the same cookie header.

pub fn set_host(req: Request(a), host: String) -> Request(a)

Set the method of the request.

pub fn set_method(req: Request(a), method: Method) -> Request(a)

Set the method of the request.

pub fn set_path(req: Request(a), path: String) -> Request(a)

Set the path of the request.

pub fn set_port(req: Request(a), port: Int) -> Request(a)

Set the port of the request.

pub fn set_query(req: Request(a), query: List(#(String, String))) -> Request(
  a,
)

Set the query of the request.

pub fn set_scheme(req: Request(a), scheme: Scheme) -> Request(a)

Set the scheme (protocol) of the request.

pub fn to_uri(request: Request(a)) -> Uri

Return the uri that a request was sent to.