内容和节点的Webhooks

真的很努力得到这个工作。 我在Contentful中有一个webhook定义设置。 当我在Contentful中发布条目时,它会向webhooks.example.com发送一个HTTP POST请求。

在该子域中,我有一个运行的NodeJS服务器来接受请求。 我查看了Contentful API文档 ,它说请求主体应该包含新发布的条目。

我已经尝试了两种接收请求的方法,这两种方法都不会给请求主体任何东西。 首先,我尝试了有意义的webhook-server NPM模块:

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); }); 

这里的请求来了,我得到消息An entry was publishedreq.body是未定义的。 如果我做了console.log(req) ,我可以看到完整的request对象,它不包含body。

于是我试着运行一个基本的Express服务器来接受所有的POST请求:

 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); }); 

再次与此,我得到Incoming request消息,但req.body是空的。 我知道这个方法是错误的,因为我没有使用我的webhook用户名/密码。

如何正确接收传入的webhook请求并获取正文内容?

contentful-webhook-server不parsing请求,以便解释为什么它不会在callback中传递给你的身体。

您的服务器似乎是正确的,但似乎内容有一个自定义jsontypes不被type-is库识别。

内容types看起来像“application / vnd.contentful.management.v1 + json”

你的服务器可能会工作,如果你使body-parser接受这个自定义的内容types。 例如 :

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

如果这样做,你可以在接受的types更具体。

作为logging :

 typeis.is('application/vnd.contentful.management.v1+json', ['json']) => false 

更简单的select是修改自定义Content-Type因为我们知道它实际上返回JSON 。 只要坚持在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(); }); 
Interesting Posts