Discover how Next.js response formatting saves you from messy, buggy code!
Why Response formatting in NextJS? - Purpose & Use Cases
Start learning this pattern below
Jump into concepts and practice - no test required
Imagine building a web app where you manually create HTTP responses for every request, writing headers, status codes, and body content by hand.
Manually formatting responses is slow, easy to mess up, and hard to maintain. You might forget headers or send wrong status codes, causing bugs and bad user experience.
Next.js provides built-in response formatting tools that automatically handle headers, status codes, and content types, making your code cleaner and more reliable.
res.statusCode = 200; res.setHeader('Content-Type', 'application/json'); res.end(JSON.stringify({ message: 'Hello' }));
return new Response(JSON.stringify({ message: 'Hello' }), { status: 200, headers: { 'Content-Type': 'application/json' } });
This lets you focus on your app logic while Next.js ensures responses are correctly formatted and fast.
When building an API route, you can quickly return JSON data with proper headers and status codes without extra boilerplate.
Manual response formatting is error-prone and tedious.
Next.js simplifies response creation with clear, consistent patterns.
This improves code quality and speeds up development.
Practice
Content-Type header in a Next.js API response?Solution
Step 1: Understand the role of headers in HTTP responses
Headers provide metadata about the response, including data format.Step 2: Identify the purpose of
This header tells the client how to interpret the response body, e.g., JSON or HTML.Content-TypeFinal Answer:
To tell the client what type of data is being sent -> Option DQuick Check:
Content-Type = Data format info [OK]
- Confusing Content-Type with HTTP method
- Thinking Content-Type sets status code
- Mixing URL path with headers
Solution
Step 1: Check the Response constructor syntax
It takes the body as first argument, and an options object with status and headers.Step 2: Verify correct headers and status usage
Headers must include 'Content-Type' with 'application/json' for JSON data, and status should be 200.Final Answer:
return new Response(JSON.stringify(data), { status: 200, headers: { 'Content-Type': 'application/json' } }) -> Option CQuick Check:
Response(body, {status, headers}) = return new Response(JSON.stringify(data), { status: 200, headers: { 'Content-Type': 'application/json' } }) [OK]
- Passing data directly without JSON.stringify
- Using wrong header keys like contentType
- Incorrect argument order in Response
export async function GET() {
const data = { message: 'Hello' };
return new Response(JSON.stringify(data), {
status: 201,
headers: { 'Content-Type': 'application/json' }
});
}What will be the HTTP status code and response body sent to the client?
Solution
Step 1: Identify the status code set in the Response
The code sets status to 201 explicitly in the Response options.Step 2: Check the response body content
The body is JSON.stringify(data), which converts { message: 'Hello' } to '{"message":"Hello"}'.Final Answer:
Status 201 with body '{"message":"Hello"}' -> Option AQuick Check:
Status and JSON body match Status 201 with body '{"message":"Hello"}' [OK]
- Assuming default status 200 instead of 201
- Confusing raw string with JSON string
- Ignoring JSON.stringify usage
export async function POST() {
const data = { success: true };
return new Response(data, {
status: 200,
headers: { 'Content-Type': 'application/json' }
});
}What is the problem with this code?
Solution
Step 1: Check the Response body type
The Response constructor expects a string, Blob, or similar, not a plain object.Step 2: Identify the fix
The object must be converted to a string using JSON.stringify before passing to Response.Final Answer:
Response body must be a string or Blob, not an object -> Option BQuick Check:
Response body type error = Response body must be a string or Blob, not an object [OK]
- Passing raw objects directly to Response
- Thinking status 200 is invalid for POST
- Adding unnecessary headers like Accept
Solution
Step 1: Confirm Response constructor usage
It takes the body string first, then an options object with status and headers.Step 2: Verify correct headers and status for plain text
Status 404 is correct for 'Not Found', and Content-Type must be 'text/plain' for plain text.Final Answer:
return new Response('Not Found', { status: 404, headers: { 'Content-Type': 'text/plain' } }) -> Option AQuick Check:
Plain text + 404 status + correct headers = return new Response('Not Found', { status: 404, headers: { 'Content-Type': 'text/plain' } }) [OK]
- Passing object instead of string for plain text
- Wrong argument order in Response
- Using contentType instead of headers key
