0
0
NextJSframework~20 mins

Request modification in NextJS - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Next.js Request Modifier 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 server action modifying a request header?

Consider this Next.js server action that modifies the request headers before processing:

export async function POST(request: Request) {
  const newHeaders = new Headers(request.headers);
  newHeaders.set('x-custom', '123');
  const modifiedRequest = new Request(request.url, {
    method: request.method,
    headers: newHeaders,
    body: await request.text()
  });
  const customHeader = modifiedRequest.headers.get('x-custom');
  return new Response(customHeader);
}

What will be the response body when this action is called?

NextJS
export async function POST(request: Request) {
  const newHeaders = new Headers(request.headers);
  newHeaders.set('x-custom', '123');
  const modifiedRequest = new Request(request.url, {
    method: request.method,
    headers: newHeaders,
    body: await request.text()
  });
  const customHeader = modifiedRequest.headers.get('x-custom');
  return new Response(customHeader);
}
A"123"
B"undefined"
C""
D"null"
Attempts:
2 left
💡 Hint

Think about how headers are copied and set in the new Request object.

📝 Syntax
intermediate
2:00remaining
Which option causes a syntax error in Next.js server action modifying request?

Identify which code snippet will cause a syntax error when trying to modify a request in a Next.js server action.

Aconst newReq = new Request(request.url, { method: 'POST', headers: newHeaders });
Bconst newHeaders = new Headers(request.headers); newHeaders.set('x', '1');
Cconst newReq = new Request(request.url, { method: 'POST', headers: request.headers.set('x', '1') });
Dconst newHeaders = new Headers(request.headers); newHeaders.append('x', '1');
Attempts:
2 left
💡 Hint

Check if the method used on headers returns a new object or modifies in place.

🔧 Debug
advanced
2:00remaining
Why does this Next.js server action fail to modify the request body?

Given this server action code:

export async function POST(request: Request) {
  const newHeaders = new Headers(request.headers);
  newHeaders.set('x-modified', 'true');
  const modifiedRequest = new Request(request.url, {
    method: 'POST',
    headers: newHeaders,
    body: request.body
  });
  const text = await modifiedRequest.text();
  return new Response(text);
}

Why does calling modifiedRequest.text() throw an error?

NextJS
export async function POST(request: Request) {
  const newHeaders = new Headers(request.headers);
  newHeaders.set('x-modified', 'true');
  const modifiedRequest = new Request(request.url, {
    method: 'POST',
    headers: newHeaders,
    body: request.body
  });
  const text = await modifiedRequest.text();
  return new Response(text);
}
AHeaders cannot be modified after the request is created, causing a runtime error.
BThe original request body is a stream that can only be read once, so passing request.body causes an error.
CThe method 'POST' is invalid in the new Request constructor, causing a syntax error.
DThe new Request constructor requires a JSON body, so passing a stream causes a type error.
Attempts:
2 left
💡 Hint

Think about how request bodies behave as streams in web APIs.

state_output
advanced
2:00remaining
What is the value of the header after this Next.js server action modifies it twice?

Analyze this server action:

export async function POST(request: Request) {
  const headers = new Headers(request.headers);
  headers.set('x-count', '1');
  headers.set('x-count', String(Number(headers.get('x-count')) + 1));
  return new Response(headers.get('x-count'));
}

What will be the response body?

NextJS
export async function POST(request: Request) {
  const headers = new Headers(request.headers);
  headers.set('x-count', '1');
  headers.set('x-count', String(Number(headers.get('x-count')) + 1));
  return new Response(headers.get('x-count'));
}
A"undefined"
B"1"
C"NaN"
D"2"
Attempts:
2 left
💡 Hint

Consider how headers.set and headers.get work sequentially.

🧠 Conceptual
expert
2:00remaining
Which statement about modifying requests in Next.js server actions is true?

Choose the correct statement about modifying incoming requests in Next.js server actions.

ATo modify headers or body, you must create a new Request object with the desired changes because Request objects are immutable.
BYou can directly modify the incoming Request object headers and body without creating a new Request.
CModifying the headers object returned by request.headers directly changes the original request headers.
DNext.js server actions automatically clone and allow mutation of the request object passed in.
Attempts:
2 left
💡 Hint

Think about immutability of web Request objects.