Jump into concepts and practice - no test required
or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Recall & Review
beginner
What is response formatting in Next.js?
Response formatting in Next.js means shaping the data or content sent back from the server or API route so it is clear and useful for the client. It often involves setting headers and structuring JSON or HTML properly.
Click to reveal answer
beginner
How do you send a JSON response in a Next.js API route?
You use the NextResponse object with the json() method, like: return NextResponse.json({ key: 'value' }); This sets the right headers and formats the data as JSON.
Click to reveal answer
intermediate
Why is setting the Content-Type header important in response formatting?
Setting Content-Type tells the browser or client what kind of data is being sent (like JSON or HTML). This helps the client understand how to process or display the response correctly.
Click to reveal answer
intermediate
What is the role of NextResponse in Next.js response formatting?
NextResponse is a helper from Next.js that helps create HTTP responses easily. It supports methods like json() to format data and set headers automatically.
Click to reveal answer
intermediate
How can you customize HTTP status codes in Next.js API responses?
You can pass a second argument to NextResponse.json() with a status property, like: NextResponse.json(data, { status: 404 }) to send a 404 Not Found status with your response.
Click to reveal answer
Which method formats a JSON response in Next.js API routes?
ANextResponse.json()
BResponse.send()
Cfetch()
DJSON.stringify()
✗ Incorrect
NextResponse.json() formats the response as JSON and sets the correct headers automatically.
What header is important to set when sending JSON data?
AContent-Type: application/json
BAccept: text/html
CAuthorization: Bearer token
DCache-Control: no-cache
✗ Incorrect
Content-Type: application/json tells the client the response is JSON data.
How do you send a 404 status with a JSON response in Next.js?
Afetch('/404')
BNextResponse.status(404).json(data)
CResponse.status(404).send(data)
DNextResponse.json(data, { status: 404 })
✗ Incorrect
Passing { status: 404 } as the second argument to NextResponse.json() sets the HTTP status code.
What does NextResponse help with?
AFetching data from APIs
BCreating and formatting HTTP responses
CRouting pages in Next.js
DStyling components
✗ Incorrect
NextResponse helps create HTTP responses with proper formatting and headers.
Why is response formatting important?
AIt changes the URL
BIt speeds up the server
CIt ensures clients understand the data sent
DIt styles the page
✗ Incorrect
Proper formatting helps clients correctly process and display the response data.
Explain how to format a JSON response with a custom status code in a Next.js API route.
Think about how NextResponse.json() accepts a second argument for status.
You got /4 concepts.
Describe why setting the Content-Type header matters when sending responses in Next.js.
Consider how browsers know what to do with the data they receive.
You got /4 concepts.
Practice
(1/5)
1. What is the main purpose of setting the Content-Type header in a Next.js API response?
easy
A. To set the status code of the response
B. To specify the HTTP method used in the request
C. To define the URL path of the API endpoint
D. To tell the client what type of data is being sent
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 Content-Type
This header tells the client how to interpret the response body, e.g., JSON or HTML.
Final Answer:
To tell the client what type of data is being sent -> Option D
Quick 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
A. return new Response(data, { statusCode: 200, contentType: 'application/json' })
B. return Response(data, 200, { 'Content-Type': 'application/json' })
C. return new Response(JSON.stringify(data), { status: 200, headers: { 'Content-Type': 'application/json' } })
D. return new Response(JSON.stringify(data), 200, { 'Content-Type': 'text/plain' })
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 C