0
0
NextJSframework~20 mins

Request parsing in route handlers in NextJS - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Next.js Request Parsing Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
component_behavior
intermediate
2:00remaining
What is the output of this Next.js route handler when receiving a POST request with JSON body?

Consider this Next.js API route handler in the app/api/data/route.js file:

export async function POST(request) {
  const data = await request.json();
  return new Response(JSON.stringify({ received: data.message }), {
    headers: { 'Content-Type': 'application/json' }
  });
}

If the client sends a POST request with JSON body {"message": "hello"}, what will be the response body?

NextJS
export async function POST(request) {
  const data = await request.json();
  return new Response(JSON.stringify({ received: data.message }), {
    headers: { 'Content-Type': 'application/json' }
  });
}
A{"message":"hello"}
B{"received":null}
C{"received":"hello"}
DSyntaxError
Attempts:
2 left
💡 Hint

Remember that request.json() parses the JSON body and returns the object.

📝 Syntax
intermediate
2:00remaining
Which option correctly parses URL query parameters in a Next.js route handler?

Given a GET request to /api/items?category=books&sort=asc, which code snippet correctly extracts the category and sort query parameters inside a Next.js route handler?

NextJS
export async function GET(request) {
  // extract query params here
}
A
const query = JSON.parse(request.url);
const category = query.category;
const sort = query.sort;
Bconst { category, sort } = request.query;
C
const params = request.params;
const category = params.category;
const sort = params.sort;
D
const url = new URL(request.url);
const category = url.searchParams.get('category');
const sort = url.searchParams.get('sort');
Attempts:
2 left
💡 Hint

Think about how to get query parameters from the full URL string.

🔧 Debug
advanced
2:00remaining
Why does this Next.js route handler throw an error when parsing JSON body?

Examine this Next.js POST route handler:

export async function POST(request) {
  const data = request.json();
  return new Response(JSON.stringify({ name: data.name }), {
    headers: { 'Content-Type': 'application/json' }
  });
}

What is the cause of the error?

NextJS
export async function POST(request) {
  const data = request.json();
  return new Response(JSON.stringify({ name: data.name }), {
    headers: { 'Content-Type': 'application/json' }
  });
}
AMissing await before request.json(), so data is a Promise, not the parsed object
Brequest.json() is not a function on the request object
CResponse constructor requires a string, but JSON.stringify returns an object
DHeaders object is missing required 'Accept' header
Attempts:
2 left
💡 Hint

Check if the JSON parsing is asynchronous and how the result is used.

state_output
advanced
2:00remaining
What is the response when parsing form data in a Next.js POST route handler?

Given this Next.js route handler:

export async function POST(request) {
  const formData = await request.formData();
  const username = formData.get('username');
  return new Response(`Hello, ${username}!`);
}

If the client sends a form with username=Alice, what is the response text?

NextJS
export async function POST(request) {
  const formData = await request.formData();
  const username = formData.get('username');
  return new Response(`Hello, ${username}!`);
}
AHello, undefined!
BHello, Alice!
CSyntaxError
DHello, [object FormData]!
Attempts:
2 left
💡 Hint

Remember how to extract values from form data.

🧠 Conceptual
expert
2:00remaining
Which statement about Next.js route handler request parsing is TRUE?

Choose the correct statement about parsing requests in Next.js route handlers.

AThe <code>request.json()</code> method returns a Promise that resolves to the parsed JSON object from the request body.
BThe <code>request.body</code> property contains the parsed JSON object directly without needing to await anything.
CTo parse URL query parameters, you can directly access <code>request.query</code> in the route handler.
DThe <code>request.formData()</code> method returns a synchronous object representing form fields.
Attempts:
2 left
💡 Hint

Think about asynchronous parsing methods and standard Web API request properties.