headers function not getting the header in app directory
Answered
Seppala Siberian Sleddog posted this in #help-forum
Seppala Siberian SleddogOP
I'm using the headers function to get the headers in a react server component in the app directory,
and this is the code for it
I'm sending this url localhost:3000/success?id=whatever, and I'm getting the value as null, can anyone help me why is that happening.
and this is the code for it
// app > success > page.tsx
import { headers } from 'next/headers';
export const dynamic = 'force-dynamic';
export default async function SuccessPage() {
const headerList = headers();
console.log(headerList.get('id'));
return <></>;
}I'm sending this url localhost:3000/success?id=whatever, and I'm getting the value as null, can anyone help me why is that happening.
Answered by joulev
id here is a query param, not a header.you access the query param with [the
searchParams prop](https://nextjs.org/docs/app/api-reference/file-conventions/page#searchparams-optional)2 Replies
@Seppala Siberian Sleddog I'm using the headers function to get the headers in a react server component in the app directory,
and this is the code for it
tsx
// app > success > page.tsx
import { headers } from 'next/headers';
export const dynamic = 'force-dynamic';
export default async function SuccessPage() {
const headerList = headers();
console.log(headerList.get('id'));
return <></>;
}
I'm sending this url localhost:3000/success?id=whatever, and I'm getting the value as null, can anyone help me why is that happening.
id here is a query param, not a header.you access the query param with [the
searchParams prop](https://nextjs.org/docs/app/api-reference/file-conventions/page#searchparams-optional)Answer
Seppala Siberian SleddogOP
Ah okay sorry 😅