Next.js Discord

Discord Forum

How to read form-encoded params in a API route (App Router)?

Unanswered
Southeastern blueberry bee posted this in #help-forum
Open in Discord
Southeastern blueberry beeOP
Basically title. I am doing a request pretty much like this:
curl --location --request GET 'http://localhost:3000/api/v1/companies' \
 --header 'Authorization: Bearer {ACCESS_TOKEN}' \
 --header 'Content-Type: application/x-www-form-urlencoded' \
 --data-urlencode 'filter=SP'
and trying to get the filter param like this:
import { getCompaniesForUser } from '@/actions/companiesActions/actions';
import { headers } from 'next/headers';
import type { NextRequest } from 'next/server';

export const dynamic = 'force-dynamic';

export async function GET(request: NextRequest) {
  const headersList = headers();
  const accessToken = headersList.get('authorization');

  const searchParams = request.nextUrl.searchParams;
  const filter = searchParams.get('filter');

  console.log(filter);

  const data = await getCompaniesForUser(accessToken);

  return Response.json({ data });
}
But filter always returns null. How can I get this param?

0 Replies