Next.js 13: How can I access the params for GET in route.ts? #47072
-
SummaryI have a function in my client to call an API in route.ts. How can I access the params Extra credit: Is it possible to pass an image Additional information// button.tsx
const res = await axios.get(`/path/to/route`, {
params: {
filename: file.name,
},
})
// route.ts
export async function GET(
request: NextRequest,
{ params }: { params: { filename: string } }
) {ExampleNo response |
Beta Was this translation helpful? Give feedback.
Replies: 6 comments 17 replies
-
|
Have you tried, request.nextUrl.searchParamsTo be more precise: import { NextRequest } from "next/server";
export async function GET(request: NextRequest) {
console.log(request.nextUrl.searchParams.get("foo"));
return new Response("Hello, Next.js!");
}Which would log GET http://localhost:3000/api/hello?foo=bar |
Beta Was this translation helpful? Give feedback.
-
|
very thanks |
Beta Was this translation helpful? Give feedback.
-
|
Thank you so much |
Beta Was this translation helpful? Give feedback.
-
|
works like a charm. |
Beta Was this translation helpful? Give feedback.
-
|
The official doc isn't very helpful, I managed to get the params with this export async function GET(request: Request) {
const { searchParams } = new URL(request.url);
const my_param = searchParams.get("my_param");
)The top answer uses |
Beta Was this translation helpful? Give feedback.
-
|
Nextjs 13, to get the dynamic route parameters from a an API route import { NextRequest, NextResponse } from "next/server"
export const dynamic = 'force-dynamic' // defaults to force-static
export async function GET(req: NextRequest, context: {params: {chain_id: string, address: string}}) {
return NextResponse.json({
message: "Hello, world!",
searchParams: req.nextUrl.searchParams,
pathname: req.nextUrl.pathname,
chain_id: context.params.chain_id,
address: context.params.address
})
} |
Beta Was this translation helpful? Give feedback.
Have you tried,
To be more precise:
Which would log
barfor this request: