user3006609 user3006609 - 5 months ago 26
JSON Question

Parsing Contentful Webhooks (Custom JSON types)

I'm currently using this Contentful-webhook-server to listen for webhooks when content is unpublished.

server.listen(30000, function(){
console.log('Contentful webhook server running on port ' + 30000)
});

server.on('ContentManagement.Entry.publish', function(req){
console.log('An entry was published!', req);
});

server.on('ContentManagement.Entry.unpublish', function(req){
console.log('An entry was unpublished!');
console.log('Deleted SOLR ID: ', req);
});


I'm trying to parse the response I've got but I can't seem to find a way to parse the custom JSON they use in their response. Should I be creating my own server with express or am I missing a way to get the response body in this example code.

Answer

The contentful-webhook-server library uses the plain node http module for the server. Thus, the req object is a readable stream that you need to buffer and parse to get the body.

Take a look at https://nodejs.org/en/docs/guides/anatomy-of-an-http-transaction/#request-body for an example.