[]previous and next page

readFormLimited

Reads a multipart/form-data or application/x-www-form-urlencoded body and checks its entries against the form caps: the number of fields and of files, the size of a file, the size of a text field and the total of the text fields.

Reads a multipart/form-data or application/x-www-form-urlencoded body and checks its entries against the form caps: the number of fields and of files, the size of a file, the size of a text field and the total of the text fields. A violation is a 413 with a JSON data.code (FORM_TOO_MANY_FIELDS, FORM_TOO_MANY_FILES, FORM_FILE_TOO_LARGE, FORM_FIELD_TOO_LARGE, FORM_FIELDS_TOO_LARGE).

The whole body is parsed into memory first, bounded by the body limit of the request (limits.maxBodyBytes, or the route's own maxBodyBytes). The caps bound the parsed result; they do not stream the parse. A streaming parser is deferred.

Import#

ts
import { readFormLimited } from "@shadow-js/dusk/server";

Signature#

ts
async function readFormLimited(
  request: Request,
  limits: FormLimitsOptions = {},
): Promise<Record<string, unknown>>

Parameters#

Name Description
request The request.
limits Caps for this read; each one overrides the app's security.limits.form.

Returns#

The fields as formToObject builds them: repeated keys become arrays, files stay File objects.

Throws#

  • HttpError 415 when the content type is not a form, 400 when the body does not parse, 413 over a cap.

Examples#

ts
export const POST = apiRoute({
  POST: async (event) => {
    const fields = await readFormLimited(event.request, { maxFileBytes: 5 * 1024 * 1024 });
    return json({ keys: Object.keys(fields) });
  },
});