gleam/http

Functions for working with HTTP data structures in Gleam.

This module makes it easy to create and modify Requests and Responses, data types. A general HTTP message type is defined that enables functions to work on both requests and responses.

This module does not implement a HTTP client or HTTP server, but it can be used as a base for them.

Types

Attributes of a cookie when sent to a client in the set-cookie header.

pub type CookieAttributes {
  CookieAttributes(
    max_age: Option(Int),
    domain: Option(String),
    path: Option(String),
    secure: Bool,
    http_only: Bool,
    same_site: Option(SameSitePolicy),
  )
}

Constructors

  • CookieAttributes( max_age: Option(Int), domain: Option(String), path: Option(String), secure: Bool, http_only: Bool, same_site: Option(SameSitePolicy), )

A HTTP header is a key-value pair. Header keys should be all lowercase characters.

pub type Header =
  tuple(String, String)

HTTP standard method as defined by RFC 2616, and PATCH which is defined by RFC 5789.

pub type Method {
  Get
  Post
  Head
  Put
  Delete
  Trace
  Connect
  Options
  Patch
  Other(String)
}

Constructors

  • Get
  • Post
  • Head
  • Put
  • Delete
  • Trace
  • Connect
  • Options
  • Patch
  • Other(String)

    Non-standard but valid HTTP methods.

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), )
pub type Response(body) {
  Response(status: Int, headers: List(Header), body: body)
}

Constructors

  • Response(status: Int, headers: List(Header), body: body)

Policy options for the SameSite cookie attribute

https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Set-Cookie/SameSite

pub type SameSitePolicy {
  Lax
  Strict
  None
}

Constructors

  • Lax
  • Strict
  • None

The two URI schemes for HTTP

pub type Scheme {
  Http
  Https
}

Constructors

  • Http
  • Https
pub type Service(in, out) =
  fn(Request(in)) -> Response(out)

Functions

pub fn cookie_defaults(scheme: Scheme) -> CookieAttributes

Helper to create sensible default attributes for a set cookie.

Note these defaults may not be sufficient to secure your application. You should consider setting the SameSite field.

https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Set-Cookie#Attributes

pub fn default_req() -> 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 expire_resp_cookie(
  resp: Response(a),
  name: String,
  attributes: CookieAttributes,
) -> Response(a)

Expire a cookie value for a client

Not the attributes value should be the same as when the response cookie was set.

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

Decode the query of a request.

pub fn get_req_cookies(
  req: Request(String),
) -> List(tuple(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_req_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_req_origin(req: Request(a)) -> Option(String)

Get the origin request header

If no "origin" header is found in the request, falls back to the "referer" header.

pub fn get_resp_cookie(
  resp: Response(Nil),
) -> List(tuple(String, String))

Deprecated. Use get_resp_cookies instead.

pub fn get_resp_cookies(
  resp: Response(Nil),
) -> List(tuple(String, String))

Fetch the cookies sent in a response

Follows the same logic as fetching the cookie from the request (i.e. badly formed cookies will be ignored)

pub fn get_resp_header(
  response: Response(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 map_req_body(
  request: Request(a),
  transform: fn(a) -> b,
) -> Request(b)

Update the body of a request using a given function.

pub fn map_resp_body(
  response: Response(a),
  transform: fn(a) -> b,
) -> Response(b)

Update the body of a response using a given function.

pub external fn method_from_dynamic(
  Dynamic,
) -> Result(Method, Nil)
pub fn method_to_string(method: Method) -> String
pub fn parse_method(s: String) -> Result(Method, Nil)
pub fn path_segments(request: Request(a)) -> List(String)

Return the non-empty segments of a request path.

pub fn prepend_req_header(
  request: Request(a),
  key: String,
  value: String,
) -> Request(a)
pub fn prepend_resp_header(
  response: Response(a),
  key: String,
  value: String,
) -> Response(a)
pub fn redirect(uri: String) -> Response(String)

Create a response that redirects to the given uri.

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

Construct a request from a URI.

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

Return the uri that a request was sent to.

pub fn response(status: Int) -> Response(Nil)

Construct an empty Response.

The body type of the returned response is Nil, and should be set with a call to set_resp_body.

pub fn scheme_from_string(scheme: String) -> Result(Scheme, Nil)

Parse a HTTP scheme from a string

Examples

> scheme_to_string("http")
Ok(Http)

> scheme_to_string("ftp")
Error(Nil)
pub fn scheme_to_string(scheme: Scheme) -> String

Convert a scheme into a string.

Examples

> scheme_to_string(Http)
"http"

> scheme_to_string(Https)
"https"
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(tuple(String, String)),
) -> Request(a)

Set the query of the request.

pub fn set_req_body(req: Request(a), body: b) -> Request(b)

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

pub fn set_req_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_resp_body(
  response: Response(a),
  body: b,
) -> Response(b)

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

pub fn set_resp_cookie(
  resp: Response(a),
  name: String,
  value: String,
  attributes: CookieAttributes,
) -> Response(a)

Set a cookie value for a client

The attributes record is defined in gleam/http/cookie

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

Set the scheme (protocol) of the request.

pub fn try_map_resp_body(
  response: Response(a),
  transform: fn(a) -> Result(b, c),
) -> Result(Response(b), c)

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.