howdy/body

Reading and decoding request bodies. Each function takes a continuation so it can be used with use. When the body cannot be read or decoded a 400 response is returned and the continuation never runs.

fn create(ctx: GuardedContext(guarded)) {
  use input <- body.json(ctx, user.new_user_decoder())
  ...
}

Values

pub const default_limit: Int

The largest body json will read, in bytes. One mebibyte.

pub fn json(
  ctx: context.Context(guarded),
  decoder: decode.Decoder(a),
  next: fn(a) -> response.Response(content.Content),
) -> response.Response(content.Content)

Read the request body and decode it as JSON with decoder.

pub fn json_from(
  ctx: context.Context(guarded),
  bits: BitArray,
  decoder: decode.Decoder(a),
  next: fn(a) -> response.Response(content.Content),
) -> response.Response(content.Content)

Decode an already-read body as JSON. json calls this after reading; use it directly when you have read the body yourself with controller.read_body.

pub fn json_with_limit(
  ctx: context.Context(guarded),
  limit: Int,
  decoder: decode.Decoder(a),
  next: fn(a) -> response.Response(content.Content),
) -> response.Response(content.Content)

Like json but with a custom size limit in bytes.

pub fn validated(
  ctx: context.Context(guarded),
  decoder: decode.Decoder(a),
  validator: fn(a) -> Result(b, List(service.FieldError)),
  next: fn(b) -> response.Response(content.Content),
) -> response.Response(content.Content)

Read the request body, decode it as JSON, then validate it. Decode failures return 400; validation failures return 422 with every field error. The continuation only runs with a validated value.

✨ Search Document