Really struggling to get this working. I have a webhook definition setup in Contentful. When I publish an entry in Contentful it sends an HTTP POST request to webhooks.example.com.
At that subdomain I have a NodeJS server running to accept the request. I've looked at the Contentful API docs, which say the request body should contain the newly published entry.
I've tried 2 methods of receiving the request, neither of which are giving me anything for the request body. First I tried the contentful-webhook-server NPM module:
var webhooks = require("contentful-webhook-server")({
path: "/",
username: "xxxxxx",
password: "xxxxxx"
});
webhooks.on("ContentManagement.Entry.publish", function(req){
console.log("An entry was published");
console.log(req.body);
});
webhooks.listen(3025, function(){
console.log("Contentful webhook server running on port " + 3025);
});
Here the request comes through and I get the message An entry was published
but the req.body
is undefined. If I do console.log(req)
instead, I can see the full request object, which doesn't include body.
So I then tried running a basic Express server to accept all POST requests:
var express = require("express"),
bodyParser = require("body-parser"),
methodOverride = require("method-override");
var app = express();
app.use(bodyParser.json({limit: "50mb"}));
app.use(bodyParser.urlencoded({extended:true}));
app.use(methodOverride("X-HTTP-Method-Override"));
app.post("/", function(req, res){
console.log("Incoming request");
console.log(req.body);
});
Again with this, I get the Incoming request
message but req.body
is empty. I know this method is wrong because I haven't used my webhook username/password.
How do I correctly receive incoming webhook requests and get the body content?
The contentful-webhook-server
does not parse the req so that would explain why it does not deliver the body to you in the callback.
Your server seem to be correct but it seems that contentful has a custom json type that is not recognized by the type-is
library.
the content-type looks like 'application/vnd.contentful.management.v1+json'
your server will probably work if you make body-parser
accept this custom content type. For example :
app.use(bodyParser.json({type: 'application/*'}));
If this works, you could be more specific on the accepted type.
For the record :
typeis.is('application/vnd.contentful.management.v1+json', ['json'])
=> false
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With