How retrieve a slug in a route (app router)
Unanswered
Chilean jack mackerel posted this in #help-forum
Chilean jack mackerelOP
Hello !
I have a route here in app : /api/[v]/auth/login/route.ts
I want to get the "slug" (don't know if it's the good term) [v] in route.ts
here is my code :
Is it even possible to do this with request? Or I need to parse the URL?
I have a route here in app : /api/[v]/auth/login/route.ts
I want to get the "slug" (don't know if it's the good term) [v] in route.ts
here is my code :
// src/app/api/[v]/auth/login/route.ts
import { NextRequest, NextResponse } from "next/server";
async function POST(request: NextRequest): Promise<NextResponse>
{
const { username, password } = await request.json();
console.log(username, password);
// here i want to check what is the version of the api for example v1
console.log(apiVersion) // Output : v1
return NextResponse.json({ message: "test" }, { status: 200 });
}
export { POST };Is it even possible to do this with request? Or I need to parse the URL?
2 Replies
hey @Chilean jack mackerel , you can actually just add a secondary param to your function arguments called
params and you can then grab the path params as if they were an object --> https://nextjs.org/docs/app/building-your-application/routing/dynamic-routesexport async function GET(
req: NextRequest,
{ params: { slug } }: { params: { slug: string } }
) {
// Do whatever you'd like here
}hope this helps