Parsing Meteor-解析来自传入HTTP请求的数据

Parsing Meteor-解析来自传入HTTP请求的数据,parsing,http,meteor,iron-router,Parsing,Http,Meteor,Iron Router,对于传出的HTTP请求(使用meteor.HTTP.call),我可以定义参数和/或数据。然后可以获得结果(通过results.content) 如何访问和解析传入HTTP请求的正文/内容/数据 使用iron router,我得到了如下信息: Router.map(function () { this.route('httpTest', { path: '/httpTest', action: function () { var request = this.request; var r

对于传出的HTTP请求(使用meteor.HTTP.call),我可以定义参数和/或数据。然后可以获得结果(通过results.content)

如何访问和解析传入HTTP请求的正文/内容/数据

使用iron router,我得到了如下信息:

Router.map(function () {
this.route('httpTest', {
path: '/httpTest',
action: function () {
  var request = this.request;
  var response = this.response;      
  console.log('request_body: '+ request.body);
  // request.body does not work.  what should it be????
注意:我知道我可以访问查询参数,但我想从传入http请求的主体中访问表单数据和/或json数据。

请求是一个,这是一个,因此您可以通过从该流读取来获取请求的数据

以下应该可以工作(但我还没有测试):

请求是一个,它是一个,因此您可以通过读取该流来获取请求的数据

以下应该可以工作(但我还没有测试):


它可能无法工作,因为缺少
其中的:'server'
。以下是一个工作示例:

Router.map(function() {
  this.route('test', {
    where: 'server',
    action: function() {
      console.log(this.request.body.make);
      console.log(this.request.body.model);
      this.response.writeHead(200, {'Content-Type': 'text/html'});
      this.response.end('hello!\n');
    }
  });
});
在命令行中,我可以使用以下命令点击此路线:

curl -X POST -H "Content-Type: application/json" -d '{"make":"honda","model":"civic"}' http://localhost:3000/test

在服务器终端中打印预期的
honda
civic
。该.request.body已被解析,因此您可以直接访问任何变量,如果您的输入是json,这很好。

它可能无法工作,因为缺少
其中的:'server'
。以下是一个工作示例:

Router.map(function() {
  this.route('test', {
    where: 'server',
    action: function() {
      console.log(this.request.body.make);
      console.log(this.request.body.model);
      this.response.writeHead(200, {'Content-Type': 'text/html'});
      this.response.end('hello!\n');
    }
  });
});
在命令行中,我可以使用以下命令点击此路线:

curl -X POST -H "Content-Type: application/json" -d '{"make":"honda","model":"civic"}' http://localhost:3000/test

在服务器终端中打印预期的
honda
civic
。它看起来像是
this.request.body
已经被解析,因此您可以直接访问任何变量,如果您的输入是json,这很好。

要读取原始的body,而无需Node.json以同步的方式对其进行加密,我使用了以下方法:

Router.route("my/api", function() {
    var rawBody = "";
    var chunk;
    while ((chunk = this.request.read()) !== null) {
        rawBody += chunk;
    }
}, { where: "server" });

(这里的另一个答案中提出的异步方式对我不起作用,尽管它应该符合Node.js文档的要求)。

要在没有Node.JSON同步的情况下读取原始正文,我使用了以下方法:

Router.route("my/api", function() {
    var rawBody = "";
    var chunk;
    while ((chunk = this.request.read()) !== null) {
        rawBody += chunk;
    }
}, { where: "server" });

(这里的另一个答案中提出的异步方式对我不起作用,尽管它应该符合Node.js文档的要求)。

这很有帮助,并为我指出了Node文档。但是,上面的代码返回“无法调用未定义route.action的“on”方法”有任何线索吗?感谢David所指出的,您缺少了“where;‘server’”,因此您正在客户端上执行此操作,而this.request似乎确实未定义,正如错误在这里告诉您的那样。是的,修复了这两种方法-我接受了David的回答,因为我确实收到了json,而且此方法简单直观。在我的例子中,“end”事件没有被触发:可能是因为流在发出readable.on(“end”)之前被消耗了?这很有帮助,并向我指出了节点文档。但是,上面的代码返回“无法调用未定义route.action的“on”方法”有任何线索吗?感谢David所指出的,您缺少了“where;‘server’”,因此您正在客户端上执行此操作,而this.request似乎确实未定义,正如错误在这里告诉您的那样。是的,修复了这两种方法-我接受了David的回答,因为我确实收到了json,而且此方法简单直观。在我的例子中,“end”事件没有被触发:可能是因为流在发出可读的.on(“end”)?
this.request.body.make
不再工作,这是
this.request.make
this.request.body.make不再工作,这是
this.request.make