Process a request body
Read the body of a request inside a function, switching on the Content-Type header to parse JSON, form data, text, binary, and streamed payloads.
Read the body a client sends and answer from the function, without forwarding the request to an origin. The Request object exposes one parsing method per format, and the Content-Type header of the request says which one to call. Use this pattern for endpoints that accept a payload: a webhook receiver, a form handler, or an upload endpoint that inspects what it receives before storing it.
This handler uses the ES Modules pattern, which Azion recommends. The rest of this collection is written in the Service Worker pattern.
How it works
The fetch method checks request.body first. A request that carries no body, such as a GET, answers with No request body was sent and never reaches a parser.
When there is a body, the handler reads the Content-Type header and routes to one parsing method. Each method consumes the body once and returns it in the shape that format implies:
Content-Type | Method | What the handler does |
|---|---|---|
application/json | request.json() | Parses the payload into an object, adds a received flag and a timestamp, and returns it as JSON |
multipart/form-data or application/x-www-form-urlencoded | request.formData() | Reads the fields into a FormData object and appends a field before returning it |
text/plain | request.text() | Reads the body as a string and echoes it |
application/octet-stream | request.blob() | Reads the body as binary and returns its text |
| Anything else | request.body.getReader() | Reads the stream chunk by chunk, decoding each chunk as UTF-8 |
The stream branch is the fallback, so an unrecognized content type is still read rather than dropped. It is also the branch to use when the body is large enough that holding the whole payload in memory matters: the reader hands over one chunk at a time, and the loop ends when done is true.
A body can be read only once. Calling two parsing methods on the same request throws, which is why the handler picks exactly one branch and returns it.
Every parsing method is awaited inside the try block, so a malformed payload is caught rather than escaping the handler. A body that does not match its declared type, such as {not json sent as application/json, answers with 400 and the parser’s own message. Returning the parser’s promise without awaiting it would put the rejection outside the try and the client would receive a runtime error instead.