Lesson 12 / 32
Parsing Request Bodies
Parse JSON, form and multipart request bodies with built-in handlers and the multipart plugin.
Content-Type driven parsing
Fastify automatically parses request bodies based on Content-Type. JSON, URL-encoded forms, and multipart are supported out of the box.
JSON request bodies
Access request.body after Fastify parses the JSON.
fastify.post('/users', async (req) => {
// req.body is already parsed from Content-Type: application/json
const { name, email } = req.body;
return { id: 1, name, email };
});File uploads with multipart
For file uploads, use the @fastify/multipart plugin to handle multipart/form-data.