I'm currently working on implementing basic CRUD operations using the latest Next.js 13 route handlers in combination with Prisma using TypeScript.
This is how my POST request handler appears:
export async function POST(req: NextRequest) {
const clientData: clientToCreate = req.body;
try {
const data = await prisma.client.create({
data: clientData,
});
return NextResponse.json(data);
} catch (e) {
console.error(e);
return NextResponse.error(e);
}
}
In this case, the req.body is
ReadableStream<Uint8Array> | null
and I'm struggling to find a way to parse it to access the contained data. Attempting req.body.json() results in: Property 'json' does not exist on type 'ReadableStream<Uint8Array>'
.
This is how I construct my request:
interface ApiRequest {
url: string;
method: "GET" | "POST" | "PUT" | "DELETE";
params?: Object;
}
const getRequestUrl = (url: string) => {
return `${process.env.BASE_API_URL}/${url}`;
};
const defaultHeaders = {
Accept: "application/json; charset=UTF-8",
"Content-Type": "application/json",
};
export const apiRequest = async (request: ApiRequest) => {
return await fetch(getRequestUrl(request.url), {
headers: defaultHeaders,
method: request.method,
body: request?.params ? JSON.stringify(request.params) : undefined,
})
.then((response) => response.json())
.catch((e) => {
console.error(e);
throw e;
});
};
After setting up the request as described above, I use Postman to send a POST request with identical headers. Can you help me identify where the issue might be?
Thank you for your assistance in advance.