我不知道这是否是确切的问题,但据我所知是这样。 我正在使用 HTTP.call 进行外部 API 调用并尝试将响应解析为 JSON(它是),但我得到了奇怪的返回内容,我认为这是 Gzipped 内容。我将 gzip 的 npmRequestOptions 参数设置为 true,但它仍然返回相同的内容。
这是我的代码:
var result;
try {
result = HTTP.call('GET', 'http://{URL}', {
params: {
key: '{SECRETKEY}',
att: '{ATTS}',
out: 'json',
},
npmRequestOptions : {gzip : true, json: true},
headers: {
// "Accept": "application/json",
'Content-Type' : 'application/json; charset=UTF-16',
"OSLC-Core-Version": "2.0",
},
});
} catch (e) {
// Got a network error, timeout, or HTTP error in the 400 or 500 range.
}
// Append data in correct experiences format.
var content = JSON.parse(result.content);
这是来自 API 调用的响应 header ,它似乎表明内容正在被 gzip 压缩:
HTTP/1.1 200 OK
Access-Control-Allow-Credentials: true
CF-RAY: 4291c15b957d4d88-PER
Content-Encoding: gzip
Content-Type: application/json; charset=utf-16
Date: Mon, 11 Jun 2018 05:42:36 GMT
ETag: W/"863e-ylc8HuIAiQAynP7anYY1GA"
Server: cloudflare
Vary: Origin, Accept-Encoding
X-Powered-By: Express
我相信 npmRequestOptions : {gzip : true, json: true} 应该有效,但事实并非如此。没有可用的其他信息,我一直在搜索一整天。
最佳答案
所以我发现这个问题的解决方案不在 Meteor 应用程序本身,而是在返回的内容中。响应是 UTF-16LE,因此我必须从响应中删除特殊字符才能对其进行 JSON 化。
result = result.content.replace(/[^A-Za-z0-9{}@&:\-_() /\\"=[\],.?!]/g, '')
关于javascript - Meteor Http 客户端 API 调用 Gzip 解压缩结果 JSON 不工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50795520/