Introducing a new way to review your PRs.

Read the announcement

node-formidable / formidable

The most used, flexible, fast and streaming parser for multipart form data. Supports uploading to serverless environments, AWS S3, Azure, GCP or the filesystem. Used in production.

View on GitHub

13 documents · Updated Nov 9, 2026

docs / structured-request-body-parsing

Structured request body parsing

Formidable turns several non-multipart request bodies into the same result model used by the form API. JSON and URL-encoded bodies become fields. An octet-stream body becomes one uploaded file whose bytes are kept as stream data and passed through the normal file lifecycle.

Representation routing

The content-type plugin system inspects the request Content-Type header and lets the matching handler claim the body. These handlers run as part of IncomingForm parsing, after any request content encoding has been decoded.

Content type selection

The built-in handlers recognize the following body representations:

Request representationContent type matchFormidable result
JSONjson, such as application/jsonOne or more fields from the decoded JSON value
URL-encodedurlencoded, such as application/x-www-form-urlencodedNamed fields from each key-value pair
Octet-streamoctet-stream, such as application/octet-streamOne file containing the raw body bytes

The selected handler installs a parser on the form. The request body then flows through that parser until the parser ends. The form emits its final end event only after the parsed fields or file writes are complete.

References: src/Formidable.js• L33-46 src/Formidable.js• L192-300 src/Formidable.js• L482-525 src/…/index.js• L1-6

Representation flow

The following flow shows how the request stream reaches a representation-specific parser and then becomes fields or file data.

flowchart LR
  Request[HTTP request body] --> Decode[Content encoding decode]
  Decode --> Match[Content-Type handler]
  Match --> JSON[JSON parser]
  Match --> URL[URL-encoded parser]
  Match --> Octet[Octet-stream pass-through]
  JSON --> Fields[Formidable fields]
  URL --> Fields
  Octet --> File[Formidable file stream]

The body representation determines the result shape. This is why a JSON request can preserve nested data while a URL-encoded request reports repeated names as field values, and an octet-stream request does not create fields at all.

JSON request bodies

JSON handling is intended for clients that send one structured value instead of form-style key-value pairs. The JSON handler recognizes the content type, parses the complete body, and assigns the decoded value as the form fields object.

JSON field conversion

The JSON parser collects incoming chunks and runs JSON decoding when the body ends. It emits one decoded value, so arrays and nested objects remain part of the field data instead of being flattened into separate names.

The handler places the decoded value into a prototype-free fields object. Unlike URL-encoded fields, JSON fields are assigned directly and are not changed into arrays when names repeat. Invalid JSON ends parsing with an error rather than producing partial fields.

References: src/…/json.js• L3-38 src/…/JSON.js• L3-30 src/Formidable.js• L217-227 test/…/json.test.js• L5-25

URL-encoded request bodies

URL-encoded handling supports the form-style representation used by application/x-www-form-urlencoded. It converts encoded key-value pairs into the same field events that a form consumer receives from other field-based handlers.

URL-encoded field conversion

The active parser buffers the body and uses URLSearchParams to decode it when the body ends. It emits one { key, value } record for each pair. The handler turns each record into a Formidable field event.

IncomingForm groups repeated names into arrays for URL-encoded data. A single name therefore produces a one-item array, while repeated names preserve their order in the resulting field array. This behavior matches the field shape used for multipart form fields; see multipart upload handling for the separate multipart path.

The repository also exposes a streaming querystring parser, but the URL-encoded handler uses the buffering QuerystringParser. The buffering choice means the full encoded body is available before field events are emitted.

References: src/…/querystring.js• L3-39 src/…/Querystring.js• L3-31 src/…/StreamingQuerystring.js• L1-10 src/Formidable.js• L217-227 src/index.js• L25-26

Octet-stream request bodies

Octet-stream handling is intended for a request whose entire body is file content rather than a set of named fields. The handler creates one file record from request metadata and sends the raw body bytes into that file.

Octet-stream file conversion

The handler reads the filename from the X-File-Name header and uses the request content type as the file MIME type. It creates the file before body data arrives, emits fileBegin, and writes each parser output chunk to the file. The parser is a pass-through stream, so the stored bytes match the request bytes.

When all writes finish, the handler closes the file and emits it under the file field name. The result therefore contains a file entry and no body fields. File storage still follows the normal Formidable file policy, including the configured disk or custom write stream.

References: src/…/octetstream.js• L3-83 src/…/OctetStream.js• L1-10 src/Formidable.js• L228-233 test/…/octet-stream.test.js• L20-53

Parser behavior comparison

The three handlers share the request and completion lifecycle, but they use different conversion rules:

ParserInput treatmentOutput event or dataResult shape
JSON parserCollects chunks and decodes one JSON value at end of bodyOne decoded valueDirect field object with nested values preserved
URL-encoded parserCollects the encoded body and decodes key-value pairs at end of bodyOne field event per pairField names with arrays for repeated names
Octet-stream parserPasses each body chunk through unchangedBinary chunks for the file writerOne file entry named file

The choice between these handlers is therefore a contract with the sender. Use JSON when the body carries structured data, URL encoding when the body carries form fields, and octet-stream when the body itself is the file.

Change checklist

When these files or sections are changed, remember to consider these:

  • Keep each content-type match aligned with its parser and with the default plugin set. A handler that no longer claims its media type can result in an unsupported request.
  • Keep the field aggregation rule aligned with the handler type. URL-encoded fields use arrays for repeated names, while JSON values keep their decoded structure.
  • Keep octet-stream file completion tied to pending writes. Emitting the file before its last write finishes can make downstream consumers read incomplete data.
  • Keep the octet-stream metadata contract aligned with clients that send X-File-Name and with the file naming and storage policy.
  • Keep parser errors connected to the form error path. A body decoder must not leave the request waiting after malformed input.

Important failure modes

TriggerConsequenceGuard
Missing or unsupported Content-TypeNo structured handler claims the body and parsing fails as unsupported mediaSend a supported JSON, URL-encoded, or octet-stream content type and keep the matching plugin enabled
Invalid JSON syntaxThe JSON parser reports an error and the form does not produce a successful resultValidate JSON before sending it or handle the form error
Large JSON or URL-encoded bodyThe active parser retains the complete body before conversion, which can increase memory useApply an appropriate request-size limit before accepting these representations
Octet-stream write failureThe file cannot complete and the form reports an error instead of a usable uploadHandle parse errors and use a working configured file destination or write stream

Key files

FileWhy to read it
src/Formidable.jsShows how request bytes reach a selected parser and how field and file results are collected.
src/…/json.jsDefines the JSON content-type handler and its mapping into form fields.
src/…/JSON.jsDefines the JSON body-to-value conversion.
src/…/querystring.jsDefines the URL-encoded handler and its field events.
src/…/Querystring.jsDefines the active URL-encoded body parser.
src/…/octetstream.jsDefines the raw-body-to-file flow and file completion rules.
src/…/OctetStream.jsDefines the pass-through behavior for binary body data.
src/index.jsExposes the handlers and parser classes to consumers.