Meteor Http 客户端 API 调用 Gzip 解压缩结果 JSON 不工作

Meteor Http Client API Call Gzip uncompress resultant JSON not working

我不知道这是否是确切的问题,但据我所知是这样。 我正在使用 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 调用的响应 headers,似乎表明内容正在压缩:

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, '')