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?
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); }
Think about how headers are copied and set in the new Request object.
The new Headers object copies all original headers, then sets 'x-custom' to '123'. The new Request uses these headers, so getting 'x-custom' returns '123'.
Identify which code snippet will cause a syntax error when trying to modify a request in a Next.js server action.
Check if the method used on headers returns a new object or modifies in place.
Headers.set() modifies the Headers object in place and returns void, so using it directly inside the headers property causes a syntax error.
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?
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); }
Think about how request bodies behave as streams in web APIs.
Request bodies are streams that can only be read once. Passing request.body to a new Request after it was already read causes an error because the stream is locked or consumed.
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?
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')); }
Consider how headers.set and headers.get work sequentially.
First, 'x-count' is set to '1'. Then it is read, converted to number 1, incremented to 2, and set again. So the final value is '2'.
Choose the correct statement about modifying incoming requests in Next.js server actions.
Think about immutability of web Request objects.
Request objects are immutable. To change headers or body, you create a new Request with the desired modifications.