Lesson 4 / 30
Request & Reply Objects
Understand the core objects that handle HTTP communication.
request and reply
request contains the incoming HTTP request details (URL, headers, body, params, query). reply is an object with methods to send the response—it's pooled for performance.
Reading request data
Access path params, query strings, headers, and body from the request object.
fastify.get('/users/:id', async (request, reply) => {
const { id } = request.params;
const { filter } = request.query;
const token = request.headers.authorization;
return { userId: id, filter, hasToken: !!token };
});Sending replies
Return an object (auto-serialized to JSON) or use reply methods for custom status codes and headers.
fastify.post('/items', async (request, reply) => {
// Auto-JSON serialization
return { id: 1, status: 'created' };
});
fastify.delete('/items/:id', async (request, reply) => {
// Custom status and location header
return reply.status(204).send();
});