首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >将JSON发布到Parse.com上的webhook

将JSON发布到Parse.com上的webhook
EN

Stack Overflow用户
提问于 2015-03-03 11:54:05
回答 1查看 517关注 0票数 0

我是webhooks的新手,正在尝试弄清楚如何接收JSON对象并将其保存到Parse.com。

下面是我的Express.js webhook的样子:

代码语言:javascript
复制
// These two lines are required to initialize Express in Cloud Code.
var express = require('express');
var app = express();

// Global app configuration section
app.set('views', 'cloud/views');  // Specify the folder to find templates
app.set('view engine', 'ejs');    // Set the template engine
app.use(express.bodyParser());    // Middleware for reading request body

app.post('/notify_message',
         // express.basicAuth('', ''),
         function(data) {
  // Use Parse JavaScript SDK to create a new message and save it.
  var Kenpom = Parse.Object.extend("Kenpom");
  var kenpom = new Kenpom();
  kenpom.save({ 
    conference : data.results,
  }).then(function(kenpom) {
    res.send('Success');
  }, function(error) {
    res.status(500);
    res.send('Error');
  });
});

// Attach the Express app to Cloud Code.
app.listen();

这是要发送到webhook的JSON对象:

代码语言:javascript
复制
{
  "name": "REST",
  "count": 351,
  "frequency": "Every 15 mins",
  "version": 198,
  "newdata": false,
  "lastrunstatus": "success",
  "lastsuccess": "Tue Mar 03 2015 02:41:02 GMT+0000 (UTC)",
  "thisversionstatus": "success",
  "nextrun": "Tue Mar 03 2015 03:29:21 GMT+0000 (UTC)",
  "thisversionrun": "Tue Mar 03 2015 02:41:02 GMT+0000 (UTC)",
  "results": {
    "collection1": [
      {
        "rank": "1",
        "team": {
          "href": "http://kenpom.com/team.php?team=Kentucky",
          "text": "Kentucky"
        },
        "conference": {
          "href": "http://kenpom.com/conf.php?c=SEC",
          "text": "SEC"
        },
        "currentrecord": "29-0",
        "pyth": ".9790",
        "offensiveefficiency": "118.7",
        "defensiveefficiency": "84.9",
        "tempo": "63.6"
      },
      {
        "rank": "2",
        "team": {
          "href": "http://kenpom.com/team.php?team=Arizona",
          "text": "Arizona"
        },
        "conference": {
          "href": "http://kenpom.com/conf.php?c=P12",
          "text": "P12"
        },
        "currentrecord": "26-3",
        "pyth": ".9649",
        "offensiveefficiency": "115.8",
        "defensiveefficiency": "86.8",
        "tempo": "66.6"
      }, ....

Parse.com接收所有列,但我在解析中看到的对象是:{}

EN

回答 1

Stack Overflow用户

发布于 2015-03-03 18:38:07

代码中一定缺少正文解析器设置。下面是一个示例代码,它在我的例子中工作得很好。看看第3行到第5行。

代码语言:javascript
复制
var Express = require('express');
var app = Express();
var parseExpressRawBody = require('parse-express-raw-body');
app.use(Express.bodyParser());  // Populate req.body
app.use(parseExpressRawBody()); //For parsing the body in JSON format

app.post('/createMessage', function(req,res){
    var data = req.body;
    var Message = Parse.Object.extend("Message");
    var message = new Message();
    message.save({ 
        type : data.type,
        text : data.text
    }).then(function(message) {
        res.send("Success");
    }, function(error) {
        res.status(500);
        res.send('Error');
    });
});

app.listen();

注意:您应该在来自客户端的HTTP POST请求中包含头部'Content-Type : application/json‘。

希望这能解决你的问题。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/28823808

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档