Response formatting helps you send data back to the user in a clear and useful way. It makes sure the information looks right and is easy to understand.
Response formatting in NextJS
Introduction
Syntax
NextJS
export async function GET(request) { return new Response(JSON.stringify({ message: 'Hello' }), { status: 200, headers: { 'Content-Type': 'application/json' } }); }
Use
Response to create a response with body, status, and headers.Always set
Content-Type header to tell the client what type of data you send.Examples
NextJS
export async function GET() { return new Response('Hello World', { status: 200, headers: { 'Content-Type': 'text/plain' } }); }
NextJS
export async function GET() { const data = { name: 'Alice', age: 30 }; return new Response(JSON.stringify(data), { status: 200, headers: { 'Content-Type': 'application/json' } }); }
NextJS
export async function GET() { return new Response(JSON.stringify({ error: 'Not found' }), { status: 404, headers: { 'Content-Type': 'application/json' } }); }
Sample Program
This Next.js API route sends a JSON response with user data. The client will receive the user info as JSON with status 200.
NextJS
export async function GET() { const user = { id: 1, name: 'John Doe' }; const json = JSON.stringify(user); return new Response(json, { status: 200, headers: { 'Content-Type': 'application/json' } }); }
Important Notes
Always stringify objects before sending in the response body.
Set the correct Content-Type header so browsers and clients know how to handle the data.
You can customize status codes to indicate success or errors.
Summary
Response formatting controls how your server sends data back.
Use Response with body, status, and headers for clear communication.
Always set Content-Type to match your data format.
Practice
1. What is the main purpose of setting the
Content-Type header in a Next.js API response?easy
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]
Hint: Content-Type always describes the data format sent [OK]
Common Mistakes:
- Confusing Content-Type with HTTP method
- Thinking Content-Type sets status code
- Mixing URL path with headers
2. Which of the following is the correct way to create a JSON response with status 200 in a Next.js API route?
easy
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]
Hint: Use new Response with JSON.stringify and proper headers [OK]
Common Mistakes:
- Passing data directly without JSON.stringify
- Using wrong header keys like contentType
- Incorrect argument order in Response
3. Consider this Next.js API handler code snippet:
What will be the HTTP status code and response body sent to the client?
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?
medium
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]
Hint: Look for status in Response options and JSON.stringify body [OK]
Common Mistakes:
- Assuming default status 200 instead of 201
- Confusing raw string with JSON string
- Ignoring JSON.stringify usage
4. You wrote this Next.js API handler:
What is the problem with this code?
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?
medium
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]
Hint: Always JSON.stringify objects before sending in Response body [OK]
Common Mistakes:
- Passing raw objects directly to Response
- Thinking status 200 is invalid for POST
- Adding unnecessary headers like Accept
5. You want to send a plain text response with status 404 from a Next.js API route. Which code snippet correctly formats this response?
hard
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]
Hint: Use string body with status and 'text/plain' header [OK]
Common Mistakes:
- Passing object instead of string for plain text
- Wrong argument order in Response
- Using contentType instead of headers key
Recommended
Ready to prove it?
Pass the test and this pattern moves unknown
10 questions ~5 mineasy hard
