I am trying to access the the query in my http request with the following url http://localhost:3000/api/hello?type=customer does anyone know how to retrive the value of type?
export async function GET(req: NextApiRequest, res: NextApiResponse) {
if (req.method !== 'GET') {
res.setHeader('Allow', ['GET']);
return res.status(405).end('Method Not Allowed');
}
const cusID = "**************";
const pmID = "******************";
console.log(req.query) // undefined
const type = req.query.type;
try {
if (type === 'paymentMethod') {
const paymentMethod = await stripe.customers.retrievePaymentMethod(cusID, pmID);
console.log(paymentMethod);
return res.status(200).json(paymentMethod);
} else if (type === 'customer') {
const customer = await stripe.customers.retrieve(cusID);
console.log('Customer:', customer);
return res.status(200).json(customer);
} else {
return res.status(400).json({ error: "No valid query parameter provided" });
}
} catch (error) {
console.error(error);
return res.status(500).json({ error: "Internal Server Error" });
}
}
Problem :
I am trying to access the the query in my http request with the following url
Possible Cause :
Possible Solution :
Use nextUrl
to access query (type) :
const type = req.nextUrl.searchParams.get('type')
Please Read :
If you have any doubts, then please leave a comment (I will update answer if necessary)