我想从Parse.com云代码中获取相同的HTTP响应,调用Parse.Cloud.httpRequest,因为我已经从cUrl获得了。但我无法得到它。有谁知道我做错了什么?
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日志中看到了不可读的数据。有可能解决它吗?
答案 0 :(得分:1)
该内容采用gzip编码。线索在响应标题中 - 有一个“Content-Encoding”:返回“gzip”标题。
如果要在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标头中传递标识来关闭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不会覆盖您设置的值。