Is there a better way to get path param in API
Answered
4:44 posted this in #help-forum
4:44OP
Let's say i have a route
Is there a better way cause this does't seem right to me? I was expecting the
/api/foo/[id]/route.ts what i normally do in a request isexport const GET = (request: NextRequest) => {
const url = new URL(request.url);
const id = url.pathname.split("/").pop();
return NextResponse.json({id});
}Is there a better way cause this does't seem right to me? I was expecting the
GET = ({param}) to get param like this but it doesn't work.Answered by Broad-snouted Caiman
Check Dynamic Route Segments on https://nextjs.org/docs/app/building-your-application/routing/route-handlers#dynamic-route-segments
5 Replies
Broad-snouted Caiman
Check Dynamic Route Segments on https://nextjs.org/docs/app/building-your-application/routing/route-handlers#dynamic-route-segments
Answer
@Broad-snouted Caiman Check Dynamic Route Segments on https://nextjs.org/docs/app/building-your-application/routing/route-handlers#dynamic-route-segments
4:44OP
Oh so it has to be the second option, got it
Broad-snouted Caiman
Yeah, it should be something like this:
export const GET = (request: NextRequest, { params }: { params: { id: string } }) => {
const id = params.id
return NextResponse.json({id});
}@4:44 So how do i mark this as solved?
Broad-snouted Caiman
Put your cursor above my reply. It will appear 3 dots on the top-right corner. Click on that, then Apps, then Mark Solution.