conversation

Types

Data parsed from form sent in a request’s body.

pub type FormData {
  FormData(
    values: List(#(String, String)),
    files: List(#(String, UploadedFile)),
  )
}

Constructors

  • FormData(
      values: List(#(String, String)),
      files: List(#(String, UploadedFile)),
    )

    Arguments

    • values

      String values of the form’s fields, sorted alphabetically.

    • files

      Uploaded files, sorted alphabetically by file name.

A standard JavaScript Request.

pub type JsRequest

A standard JavaScript Response.

pub type JsResponse

Error type representing possible errors produced by body reading functions.

pub type ReadError {
  AlreadyRead
  ParseError(message: String)
  ReadError(message: String)
}

Constructors

  • AlreadyRead

    Request body has already been read.

  • ParseError(message: String)

    Failed to parse JSON body.

  • ReadError(message: String)

    Failed to read request body.

The body of an incoming request. It must be read with functions like read_text, and can only be read once.

pub type RequestBody

Body type for an outgoing response.

import gleam/http/response
import conversation.{Text}

response.new(200)
|> response.set_body(Text("Hello, world!"))
pub type ResponseBody {
  Text(String)
  Bits(BitArray)
}

Constructors

  • Text(String)
  • Bits(BitArray)

File uploaded from the client.

pub type UploadedFile {
  UploadedFile(file_name: String, path: String)
}

Constructors

  • UploadedFile(file_name: String, path: String)

    Arguments

    • file_name

      The name that was given to the file in the form. This is user input and should not be trusted.

    • path

      The location of the file on the server. This is a temporary file and will be deleted when the request has finished being handled.

Functions

pub fn read_bits(
  body: RequestBody,
) -> Promise(Result(BitArray, ReadError))

Read a request body as a BitArray.

pub fn read_form(
  body: RequestBody,
) -> Promise(Result(FormData, ReadError))

Read a request body as FormData.

pub fn read_json(
  body: RequestBody,
) -> Promise(Result(Dynamic, ReadError))

Read a request body as JSON, returning a Dynamic value which can then be decoded with gleam_json. If the JSON cannot be parsed, a ParseError is returned.

pub fn read_text(
  body: RequestBody,
) -> Promise(Result(String, ReadError))

Read a request body as text.

pub fn translate_request(req: JsRequest) -> Request(RequestBody)

Translates a JsRequest into a Gleam Request.

pub fn translate_response(
  res: Response(ResponseBody),
) -> JsResponse

Translates a Gleam Response into a JsResponse.

Search Document