0
votes

I'm attempting to receive a webhook from a 3rd party. Whilst I can see the content-length > 0, console.logging the req.body simply yields {}. The request is being sent to the route '/v2/wtevr/report/wtevr'.

These are the headers received from the webhook POST request:

accept: '*/*',
'accept-encoding': 'gzip, deflate',
'user-agent': 'rest-client/2.0.2 (linux-gnu x86_64) ruby/2.5.3p105',
'content-type': 'application/vnd.wtevr.wtevr.leadwebhook+json;version=0.0.2',
'content-length': '254',
host: 'api.mysite.co.uk'

I'm using Express' body-parser to parse the response. According to the Express docs, body-parser supports automatic inflation of 'gzip' and 'deflate' encodings. I've specified the content-type to catch the request and unzip it, but it's not working. This is what my code looks like:

app.use(
  function(req, res, next) {
    if (req.url === '/v2/wtevr/report/wtevr') {
      next();
    }
  }
)
app.use(bodyParser.json({type: ['application/json', 'application/vnd.wtevr.wtevr.leadwebhook+json;version=0.0.2']}));
app.use(bodyParser.urlencoded({ extended: true }));

Does anyone know how I can parse/view the body?

1

1 Answers

1
votes

Managed to solve my own question. The solution was to not specify the custom content-type as an exact string in the 'type' option of the body-parser's .json function, but to either use a wildcard or to specify it exactly but as a function.

Either of the two code snippets below work:

app.use(bodyParser.json({type: (req) => req.get('Content-Type') === 'application/vnd.wtevr.wtevr.leadwebhook+json;version=0.0.2'}));
app.use(bodyParser.json());

or

app.use(bodyParser.json({type: ['application/json', 'application/*+json']}));