gleam/http

Functions and types for working with HTTP Gleam.

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 must be all lowercase characters.

pub type Header =
  #(String, String)

HTTP standard methods as defined by RFC 2616, RFC 5789, and RFC 10008.

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

Constructors

  • Get

    GET requests a resource from a server. It is safe, idempotent, cachable, and should not have a request body.

  • Post

    POST sends data to a server.

  • Patch

    PATCH performs a partial update to a resource on a server.

  • Put

    PUT sends data to a server. It is idempotent.

  • Delete

    DELETE deletes a resource from a server. It should not have a request body.

  • Head

    HEAD requests metadata about a resource from a server. It is safe, idempotent, cachable, and should not have a request body.

  • Options

    TRACE performs a message loop-back test along the path to the target resource. It should not have a request body. OPTIONS requests information about what requests are permitted to a resource, i.e. HTTP methods and CORS.

  • Trace
  • Connect

    CONNECT requests that a proxy connects to a specified server and forward data to and from it. It should not have a request body or a response body.

    Proxies that support this should only do so for known trustworthy clients.

  • Other(String)

    Non-standard but valid HTTP methods.

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),
    )

    Arguments

    continuation

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

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

Constructors

  • MultipartHeaders(
      headers: List(#(String, String)),
      remaining: BitArray,
    )

    The headers for the part have been fully parsed. Header keys are all lowercase.

    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

Values

pub fn method_to_string(method: Method) -> String
pub fn parse_content_disposition(
  header: String,
) -> Result(ContentDisposition, Nil)
pub fn parse_method(method: 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

assert Ok(Http) == scheme_from_string("http")
assert Error(Nil) == scheme_from_string("ftp")
pub fn scheme_to_string(scheme: Scheme) -> String

Convert a scheme into a string.

Examples

assert "http" == scheme_to_string(Http)
assert "https" == scheme_to_string(Https)
Search Document