Next.js POST API route
POST Route with JSON
URL: domain.com/api/myroute
File path: src > app > api > my route > route.ts
import { NextRequest, NextResponse } from "next/server";
export const POST = async (request: NextRequest) => {
// sending your data as a json package
// get the sent JSON data (body) from the request and destructure it
const {
title,
gender,
category,
} = await request.json();
// your logic here
// your logic here
// return the in data as a JSON response
return NextResponse.json(
{
message: "Connected to endpoint",
title,
gender,
category,
// you can return any data here
},
{
status: 200,
headers: {
"Content-Type": "application/json",
},
}
);
}