使用Parse.Cloud.httpRequest我无法为AWS请求设置正确的标头?

时间:2015-02-18 11:15:01

标签: json amazon-web-services parse-platform header httprequest

在Parse.com上,来自CloudCode我需要将httpRequest发送到AWS(Kinesis);它们是签名的,从浏览器端(扩展名)发送后一切正常。我在CloudCode的请求中尝试了以下标头内容类型:

"content-type": "application/json"
  

AWS拒绝了它:{“输出”:{“_ _ type”:“com.amazon.coral.service #UnknownOperationException”,“message”:null},“Version”:“1.0”}

"content-type": "application/x-amz-json-1.1"
  

parse.cloud.httprequest拒绝了它:结果:未捕获错误:不知道如何将httpRequest body对象转换为application / x-amz-json-1.1。要发送原始字节,请将httpRequest主体分配给包含数据的Buffer对象。

"content-type": "application/x-www-form-urlencoded"
  

AWS拒绝了它:请求失败,回复代码为403

AWS文档提到“application / x-amz-json-1.1”内容类型无处不在,我看不到任何替代方案。所以,我认为这是要走的路,所以:

如何让Parse.Cloud.httpRequest使用此X-AMZ-JSON标头发送请求,但在内部使用“application / json”?

更新:我尝试使用Node中的“http”和“xmlhttp”模块;但他们都没有工作 - 我很乐意尝试你的任何建议。

更新:这是我正在提出的实际要求

Parse.Cloud.httpRequest({
  method: 'POST',
  url: awsKinesisUrl,
  headers: {
    "Authorization": concat_string,
    "action": "PutRecord",
    "acl": "public-read",
    'awsaccesskeyid': "__XXX__",
    "content-type": "application/x-amz-json-1.1",
    "dategenerated":  date_generated+"",
    "region": "eu-west-1",
    "version": "2013-12-02",
    "X-Amz-Date": date_generated_TZ+"",
    "X-Amz-Target": "Kinesis_20131202.PutRecords"

  },
  body: {
    body: http_req_body
  },
  success: function(httpResponse) {
    var message = httpResponse.text;
    res.render('json', {message: '{response:\''+message+'\'}' });                       
  },
  error: function(httpResponse) {
    var message = httpResponse.status;
    res.render('json', {message: '{response:\''+message+'\'}' });                       
  }
});                                                             

1 个答案:

答案 0 :(得分:1)

查看有关通过httpRequest发送原始字节的博文:http://blog.parse.com/2013/04/04/sending-bytes-from-cloud-code/

如果http_req_body是JS对象,则可以使用以下方法调用:

// Require can go at the global scope
var Buffer = require('buffer').Buffer;
Parse.Cloud.httpRequest({
  method: 'POST',
  url: awsKinesisUrl,
  headers: {
    "Authorization": concat_string,
    "action": "PutRecord",
    "acl": "public-read",
    'awsaccesskeyid': "__XXX__",
    "content-type": "application/x-amz-json-1.1",
    "dategenerated":  date_generated+"",
    "region": "eu-west-1",
    "version": "2013-12-02",
    "X-Amz-Date": date_generated_TZ+"",
    "X-Amz-Target": "Kinesis_20131202.PutRecords"
  },
  body: new Buffer(JSON.stringify(http_req_body))
}).always(function(httpResponse) {
  var message = httpResponse.text;
  res.render('json', {message: '{response:\''+message+'\'}' });                       
});

我还冒昧地用更新的Promise技术(http://blog.parse.com/2013/01/29/whats-so-great-about-javascript-promises/)替换你的成功/错误回调。这不仅可以让您更轻松地管理多个API请求,还可以整合您为成功和错误条件重写的处理程序。