I've been attempting to access req.body within my API, but for some reason I am unable to retrieve it. Upon checking the request tab in the console, I noticed that the string I need is present.
Additionally, here is another image
.
Here is the code snippet:
export default async function handler(
req: NextApiRequest,
res: NextApiResponse
) {
if (req.method === "DELETE") {
const session = await getServerSession(req, res, authOptions)
if (!session) {
return res
.status(401)
.json({ message: "Please sign in" })
}
// Validate ID
const postId = req.body
console.log(postId)
if (!postId) {
return res
.status(400)
.json({ message: "Post ID is missing", body: postId })
}
// Delete Post
try {
const result = await prisma.post.delete({
where: {
id: postId,
}
})
res.status(200).json(result)
} catch (err) {
res.status(500).json({ message: "Error has occured while deleting the post", body: postId})
}
}
}
I have made an attempt to retrieve the postId using req.body.