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

标签 javascript json meteor gzip

我不知道这是否是确切的问题,但据我所知是这样。 我正在使用 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);

错误如截图所示: Returned content is weird in 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/

相关文章:

javascript - Angular 2 typescript : concat() work as mutable

javascript - 在 Rails View 中加载页面特定的 JS 和 CSS 文件

javascript - 使用 jquery 解析 JSON 响应中的项目

ios - 如何从 Objective C 中的 JSONP 中删除回调参数以使其可用?

javascript - 如何修改服务调用的数据响应

javascript - 我如何在更改路线时为页面制作动画?

javascript - 显示为原始 HTML 代码的表情符号

javascript - Css 在 javascript 打印功能中不起作用

mongodb - meteor - MongoDB 错误 : Cannot apply $addToSet modifier to non-array

javascript - MediaSource 从不在 React 中发出 sourceopen 事件