Skip to main content

Body Parser

Prism can parse request bodies in four formats out of the box: JSON, URL-encoded forms, XML, and multipart. The PrismBodyParserMiddleware auto-detects the content type, and request extensions give you direct access to parsed data.

Auto-Detection Middleware

Enable Body Parsing
The middleware reads Content-Type and tags the request in userInfo["parsedBodyType"]:

JSON Bodies

The most common format. Use decodeJSON() for typed decoding:
Parse JSON

URL-Encoded Forms

Standard HTML form submissions use application/x-www-form-urlencoded. Access fields through formData:
Parse Form Data

Nested Forms

For complex forms with bracket notation like user[name]=Alice&user[address][city]=Paris:
Nested Form Parsing
You can also parse nested forms directly:
Direct Nested Parsing

XML Bodies

Parse XML payloads into a traversable tree of PrismXMLNode:
Parse XML
PrismXMLNode provides:
  • name — element tag name
  • attributes — dictionary of XML attributes
  • text — text content of the element
  • children — child nodes
  • child("name") — first child with given name
  • childrenNamed("name") — all children with given name
XML with Attributes
You don’t need PrismBodyParserMiddleware to use the parsing extensions — decodeJSON(), formData, xmlBody, and nestedFormData work on any request with a body. The middleware just adds the content type tag for downstream logic.
XML parsing uses Foundation’s XMLParser under the hood — fully native, no dependencies. For large XML payloads, consider streaming the body in chunks instead.