Victor Baranov
Victor Baranov

Reputation: 155

Parse.Cloud.httpRequest httpresponse does not match cUrl response

I would like to get the same HTTP response from Parse.com cloud code calling Parse.Cloud.httpRequest as I've got from cUrl. But I can't get it. Does anybody know what am I doing wrong?

cUrl example:

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

response from cUrl:

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

Parse cloud code example:

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);
  });

Logs for HTTP response from Parse look like:

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"}

I see unreadable data in Parse logs from response. Is it possible to fix it?

Upvotes: 1

Views: 448

Answers (1)

rickerbh
rickerbh

Reputation: 9911

That content is gzip encoded. The clue is in the response headers - there is a "Content-Encoding":"gzip" header returned.

If you want the same output in CURL, try the following command

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"

You can turn off gzip encoding by passing identity in the Accept-Encoding header.

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"

Or in your cloud code...

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) {
  ....

I haven't tested this, but hopefully Parse doesn't overwrite the value you set.

Upvotes: 1

Related Questions