【问题标题】:using Parse.Cloud.httpRequest I am unable to set correct header for AWS request?使用 Parse.Cloud.httpRequest 我无法为 AWS 请求设置正确的标头?
【发布时间】:2015-02-18 11:15:01
【问题描述】:

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

"content-type": "application/json"

AWS 拒绝它:{"Output":{"__type":"com.amazon.coral.service#UnknownOperationException","message":null},"Version":"1.0"}

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

parse.cloud.httprequest 拒绝它:结果:未捕获错误:不知道如何将 httpRequest 正文对象转换为 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+'\'}' });                       
  }
});                                                             

【问题讨论】:

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


    【解决方案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 请求,还可以整合您为成功和错误条件重写的处理程序。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-02-05
      • 2016-11-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多