Next.js Discord

Discord Forum

API request body completely empty

Unanswered
Standard Chinchilla posted this in #help-forum
Open in Discord
Avatar
Standard ChinchillaOP
The JSON.stringify(req) just returns {}.

I'm using postman to post a body, but yeah its like its not receiving anything at all, although the endpoint IS being hit. Anyone know how I can fix this?


export async function POST(req: NextApiRequest, res: NextApiResponse) {
  const sgMail = require("@sendgrid/mail");

  console.log("request is " + JSON.stringify(req));

  const emailData: sendGridEmailDTO = req.body;

  console.log("emailData is " + JSON.stringify(emailData));

  sgMail
    .send(emailData)
    .then(() => {
      console.log("Email sent");
    })
    .catch((error: any) => {
      console.log("THERE WAS AN ERROR");
      console.log("error is " + JSON.stringify(error));
      console.error(error);
    });
}

3 Replies

Avatar
joulev
1. it's not req: NextApiRequest, res: NextApiResponse. The syntax is completely different now https://nextjs.org/docs/app/building-your-application/routing/router-handlers
2. to get json body: https://nextjs.org/docs/app/building-your-application/routing/router-handlers#request-body
Avatar
Standard ChinchillaOP
omg
thank you so much