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

pub type ContentDisposition {
  ContentDisposition(String, parameters: List(#(String, String)))
}

Constructors

  • ContentDisposition(String, parameters: List(#(String, String)))

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

pub type Header =
  #(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.

Call this function to continue parsing the headers for this part.

pub type MultipartBody {
  MultipartBody(chunk: BitArray, done: Bool, remaining: BitArray)
  MoreRequiredForBody(
    chunk: BitArray,
    continuation: fn(BitArray) -> Result(MultipartBody, Nil),
  )
}

Constructors

  • MultipartBody(chunk: BitArray, done: Bool, remaining: BitArray)

    The body for the part has been fully parsed.

    Arguments

    • done

      This is True if this was the last part in the multipart message, otherwise there are more parts to parse.

    • remaining

      The remaining content that has not yet been parsed. This will contain the next part if done is False, otherwise it will contain the epilogue, if any.

  • MoreRequiredForBody(
      chunk: BitArray,
      continuation: fn(BitArray) -> Result(MultipartBody, Nil),
    )

    This is True if this was the last part in the multipart message, otherwise there are more parts to parse. The remaining content that has not yet been parsed. This will contain the next part if done is False, otherwise it will contain the epilogue, if any.

    Arguments

    • continuation

      Call this function to continue parsing the body for this part.

pub type MultipartHeaders {
  MultipartHeaders(headers: List(Header), remaining: BitArray)
  MoreRequiredForHeaders(
    continuation: fn(BitArray) -> Result(MultipartHeaders, Nil),
  )
}

Constructors

  • MultipartHeaders(headers: List(Header), remaining: BitArray)

    The headers for the part have been fully parsed.

    Arguments

    • remaining

      The remaining content that has not yet been parsed. This will contain the body for this part, if any, and can be parsed with the parse_multipart_body function.

  • MoreRequiredForHeaders(
      continuation: fn(BitArray) -> Result(MultipartHeaders, Nil),
    )

    More input is required to parse the headers for this part.

    Arguments

    • continuation

      Call this function to continue parsing the headers for this part.

The two URI schemes for HTTP

pub type Scheme {
  Http
  Https
}

Constructors

  • Http
  • Https

Functions

pub fn method_from_dynamic(
  value: Dynamic,
) -> Result(Method, List(DecodeError))
pub fn method_to_string(method: Method) -> String
pub fn parse_content_disposition(
  header: String,
) -> Result(ContentDisposition, Nil)
pub fn parse_method(s: String) -> Result(Method, Nil)
pub fn parse_multipart_body(
  data: BitArray,
  boundary: String,
) -> Result(MultipartBody, Nil)

Parse the body for part of a multipart message, as defined in RFC 2045. The body is everything until the next boundary. This function is generally to be called after calling parse_multipart_headers for a given part.

This function will accept input of any size, it is up to the caller to limit it if needed.

To enable streaming parsing of multipart messages, this function will return a continuation if there is not enough data to fully parse the body, along with the data that has been parsed so far. Further information is available in the documentation for MultipartBody.

pub fn parse_multipart_headers(
  data: BitArray,
  boundary: String,
) -> Result(MultipartHeaders, Nil)

Parse the headers for part of a multipart message, as defined in RFC 2045.

This function skips any preamble before the boundary. The preamble may be retrieved using parse_multipart_body.

This function will accept input of any size, it is up to the caller to limit it if needed.

To enable streaming parsing of multipart messages, this function will return a continuation if there is not enough data to fully parse the headers. Further information is available in the documentation for MultipartBody.

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

Parse a HTTP scheme from a string

Examples

scheme_from_string(“http”) Ok(Http)

scheme_from_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”

Search Document