# NextRequest & NextResponse

> The Next.js extensions to standard Web APIs.

Subject: Next.js | Lesson 46 of 65 | 3 min read
Interactive lesson: https://codewithanimation.in/nextjs/web-request-response
Previous: [Static vs Dynamic Handlers](https://codewithanimation.in/nextjs/static-vs-dynamic-handlers.md) | Next: [<Image> (Local)](https://codewithanimation.in/nextjs/image-local.md)

## Extending Web Standards

Instead of using Node.js's `IncomingMessage` or Express's `Request` objects, Next.js uses the modern Web API standards (the same ones used in the `fetch` API).

It then extends them with `NextRequest` and `NextResponse` to add powerful Next.js-specific helpers.

## Quality of Life

Native Web Requests require you to manually parse raw cookie strings.

`NextRequest` gives you a convenient `req.cookies.get('token')` method.

It also includes `req.nextUrl`, which parses the URL for you, making it trivial to read query parameters or pathnames.

## Usage in Route Handlers

You import them from `next/server`.

To read JSON bodies: `const data = await req.json();` (Notice it is an async method).

To send JSON: `return NextResponse.json({ success: true });`

## Common mistake: The Express Muscle Memory Trap

Developers moving from Express to Next.js constantly encounter errors when they instinctively type `req.body` (which is a ReadableStream in Next.js, not a parsed object).

They also try to use `res.status(200).send(data)`, which is completely invalid in the Web Response API.

## Try it: Express vs Next.js

Examine the difference between legacy Express.js syntax and modern Next.js (Web API) syntax when handling an API request.

The animation is on the lesson page and is not part of this text version: https://codewithanimation.in/nextjs/web-request-response

## Check yourself

### Question 1

How do you extract a JSON payload from an incoming POST request in a Next.js Route Handler?

- A. const data = req.body;
- B. const data = req.json();
- C. const data = await req.json();
- D. const data = JSON.parse(req.body);

Correct answer: C

Next.js uses standard Web Request APIs. Reading the JSON body requires calling the async `.json()` method on the request object.

- Why not A: req.body is an unparsed ReadableStream.
- Why not B: json() returns a Promise, so you must await it.
- Why not D: You cannot synchronously parse a stream.

## Remember this

- `NextRequest` and `NextResponse` extend standard Web APIs.
- Use `req.cookies` and `req.nextUrl` for convenient data extraction.
- Do not use Express syntax like `req.body` or `res.send()`.
- Always `await req.json()` to parse incoming JSON payloads.
