简体   繁体   中英

Webhooks with Contentful and node

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

An easier option is to modify the custom Content-Type since we know it actually returns JSON . Just stick this somewhere above the bodyParser

app.use(function(req, res, next) {     
    if (req.headers['content-type'] === 'application/vnd.contentful.management.v1+json') req.headers['content-type'] = 'application/json';
    next();
});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM