Search code examples
next.jsvercel

Vercel serverless function not running axios post request


I am using Vercel serverless function to run a post request to webhook. This works correctly on localhost but not working after deploying to Vercel serverless function.

async function formSubmission(req, res) {
  res.statusCode = 200;
  console.log('form-submission-init');
  axios({
    method: 'POST',
    url: 'https://flow.zoho.in/*',
    data: req.body,
  })
    .then((response) => {
      console.log('success');
    })
    .catch((error) => {
      console.log('fail', error);
    });
  res.json({ data: 'done' });
}

Vercel logs form-submission-init Vercel logs do not print either fail or success

I have gone through Vercel's documentation on why it may not work link but unsure. Any help appreciated.


Solution

  • You have async flow problem, you are sending response res.json before axios promise is actually resolved.

    You either need to await axios request, or put res.json inside promise chain.