Reputation: 438
UPDATE
It looks like the 3rd party app is sending the data with Content-Type: text/json
. I need to figure out how to configure express middleware to accept */*
Content-Type
I am trying to expose an endpoint in Express to receive a post request (webhook notification) from a 3rd party app. Essentially, when a user clicks a button in the 3rd party app it sends a post request to the express endpoint with data.
This is what my code looks like:
app.use(express.json()); // for parsing application/json
app.use(express.urlencoded({ extended: true })); // for parsing application/x-www-form-urlencoded
app.post('/notify', (req, res, next) => {
if (!req.body) {
return res.status(400).send('Bad Request');
}
console.log(req.body);
res.sendStatus(200);
});
When I post to this endpoint with Postman (Postman w/ payload --> My Endpoint), everything works and the payload is present in req.body
contains data.
When the 3rd party data posts to this endpoint (3rd Party w/ payload --> My Endpoint), the body contains nothing and when logged it prints {}
.
If I use an online tool like https://webhook.site to receive the data from the 3rd party (3rd Party w/ payload --> Webhook test site), the entire payload is present in the req.body
.
I have no idea why it doesn't work and have tried to change the middleware multiple different times.
I have tried the following links but none worked.
Express app empty request body with custom content type headers
Express receiving empty object
Node.js: Receiving empty body when submitting form.
Upvotes: 2
Views: 828
Reputation: 438
The issue is the 3rd party app was sending the raw json data with the content-type header as text/plain
. I fixed the issue with:
app.use(express.json({ type: '*/*' }));
Upvotes: 4