Lesson 12 / 30

Parsing Request Bodies

Handle JSON, form data, and raw bodies with built-in content-type handlers.

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.