Parse.Cloud.httpRequest httpresponse 与 cUrl 响应不匹配

Parse.Cloud.httpRequest httpresponse does not match cUrl response

我想从 Parse.com 云代码调用 Parse.Cloud.httpRequest 获得与我从 cUrl 获得的相同的 HTTP 响应。但我无法得到它。有人知道我做错了什么吗?

cUrl 示例:

curl https://api.chain.com/v2/bitcoin/transactions/917673efa435b483343ddfe373995df365260c617107d3f9de68abd4e97c981b/confidence?api-key-id=e8d5ed773cf24e31c149ae874eb23c74

来自 cUrl 的响应:

{
 "transaction_hash":"917673efa435b483343ddfe373995df365260c617107d3f9de68abd4e97c981b",
 "block_hash":"0000000000000000042568aae3c297f687caa1b7737d8dab8e7d7b5087fb87f5",
 "propagation_level":null,
 "double_spend":null
}

解析云代码示例:

Parse.Cloud.httpRequest({
  method: 'GET',
  url: "https://api.chain.com/v2/bitcoin/transactions/917673efa435b483343ddfe373995df365260c617107d3f9de68abd4e97c981b/confidence",
  params: {
    "api-key-id": "e8d5ed773cf24e31c149ae874eb23c74"
  },
  headers: {
    'Content-Type': 'application/json'
  }
}).then(function (httpResponse) {
    console.log("httpResponse:");
    console.log("status:" + httpResponse.status);
    console.log("text:" + httpResponse.text);
    console.log("headers:");
    console.log(httpResponse.headers);
  }, function (httpResponseError) {
      console.error('httpResponse failed with response code ');
      console.log(httpResponseError);
  });

来自 Parse 的 HTTP 响应日志如下所示:

I2015-05-28T15:49:12.545Z]httpResponse:
I2015-05-28T15:49:12.547Z]status: 200
I2015-05-28T15:49:12.549Z]text: \
0D%gM?#M-
QR^:0觹ovҦٌ&YZȁg~}m%H,N-p]%DD%
qTdG,|֟u[VeՇV3rpi_w{
I2015-05-28T15:49:12.549Z]headers:
I2015-05-28T15:49:12.550Z]{"Access-Control-Allow-Credentials":"true","Access-Control-Allow-Methods":"GET,POST,PATCH,PUT,DELETE,OPTIONS,HEAD","Access-Control-Allow-Origin":"","Access-Control-Expose-Headers":"Next-Range","Connection":"keep-alive","Content-Encoding":"gzip","Content-Length":"187","Content-Type":"application/json; charset=utf-8","Date":"Thu, 28 May 2015 15:49:12 GMT","Server":"Cowboy","Strict-Transport-Security":"max-age=31536000","Vary":"Accept-Encoding","Via":"1.1 vegur","X-Content-Type-Options":"nosniff","X-Frame-Options":"SAMEORIGIN","X-Request-Id":"e2c69578-635d-4d6c-85bf-f1b0e314ea58","X-Runtime":"0.008468","X-Xss-Protection":"1; mode=block"}

我在响应的 Parse 日志中看到不可读的数据。可以修复吗?

该内容是 gzip 编码的。线索在响应 headers - 返回了 "Content-Encoding":"gzip" header。

如果你想在 CURL 中得到相同的输出,试试下面的命令

curl -X "GET" "https://api.chain.com/v2/bitcoin/transactions/917673efa435b483343ddfe373995df365260c617107d3f9de68abd4e97c981b/confidence?api-key-id=e8d5ed773cf24e31c149ae874eb23c74" \
-H "Accept-Encoding: gzip" \
-H "Content-Type: application/json"

您可以通过在 Accept-Encoding header 中传递身份来关闭 gzip 编码。

curl -X "GET" "https://api.chain.com/v2/bitcoin/transactions/917673efa435b483343ddfe373995df365260c617107d3f9de68abd4e97c981b/confidence?api-key-id=e8d5ed773cf24e31c149ae874eb23c74" \
-H "Accept-Encoding: identity" \
-H "Content-Type: application/json"

或者在您的云代码中...

Parse.Cloud.httpRequest({
  method: 'GET',
  url: "https://api.chain.com/v2/bitcoin/transactions/917673efa435b483343ddfe373995df365260c617107d3f9de68abd4e97c981b/confidence",
  params: {
    "api-key-id": "e8d5ed773cf24e31c149ae874eb23c74"
  },
  headers: {
    'Content-Type': 'application/json',
    'Accept-Encoding': 'identity'
  }
}).then(function (httpResponse) {
  ....

我还没有对此进行测试,但是希望 Parse 不会覆盖您设置的值。