How do I get the dynamic route value for an API handler?
Answered
Exotic Shorthair posted this in #help-forum
Exotic ShorthairOP
Hello gurus,
I'm attempting to load a "segment" through a fetch request via an API endpoint fetch request in Next.js 13. So far I can get the GET handler to call server side via a client component (yay!). However, I'm struggling to figure out how to get the UUID in the url. I could get that by splitting the url, yet is there a better way?
I'm attempting to load a "segment" through a fetch request via an API endpoint fetch request in Next.js 13. So far I can get the GET handler to call server side via a client component (yay!). However, I'm struggling to figure out how to get the UUID in the url. I could get that by splitting the url, yet is there a better way?
// src/app/api/segments/[uuid]/route.ts
import { update } from '@/components/data/SegmentJsonConnector';
import { NextRequest, NextResponse } from "next/server";
import { Segment } from '@/components/data/model/Segment';
import { SegmentResponseData } from '../route';
// Get a single segment
export async function GET(req: NextRequest, res: NextResponse<SegmentResponseData>) {
try {
// 🤩 I can get here so far! How do I get the UUID from /api/segments/[uuid]?
console.log(req.url);
} catch(err) {
console.log('segments api GET error', err);
return NextResponse.json(
{ error: 'An API access error occurred. Tell an adult!' },
{ status: 500 }
);
}
}2 Replies
Exotic ShorthairOP
@joulev That did the trick. I feel silly for missing it, yet that made life much more wonderful just now. Thank you for your time! 🤩