流星Http客户端API调用Gzip解压缩结果JSON无法正常工作 [英] Meteor Http Client API Call Gzip uncompress resultant JSON not working

查看:332
本文介绍了流星Http客户端API调用Gzip解压缩结果JSON无法正常工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不知道这是否是确切的问题,但据我了解. 我正在使用HTTP.call进行外部API调用,并尝试将响应解析为JSON(它是),但是我返回的是奇怪的内容,我认为这是Gzipped内容.我正在将gzip的npmRequestOptions参数设置为true,但是它仍返回相同的内容.

I don't know if it's the exact issue, but from what I can understand it is. I am making an external API call with the HTTP.call and trying to parse the response as JSON (which it is), but I'm getting strange content returned, which I think is Gzipped content. I'm setting the npmRequestOptions parameter for gzip to true, but it is still returning the same content.

这是我的代码:

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调用的响应标头,似乎表明该内容已压缩:

Here are the response headers from the API call which seems to indicate the content is being gzipped:

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}应该可以,但是不能.没有可用的其他信息,我整天都在搜索.

I believe npmRequestOptions : {gzip : true, json: true} should be working, but it's not. There's no additional information available and I've been searching all day.

推荐答案

所以我发现解决此问题的方法不在Meteor应用程序本身,而是在返回的内容中.响应为UTF-16LE,因此我必须从响应中删除特殊字符,以便对其进行JSON化.

So I've figured out the solution to this was not in the Meteor app itself, it was in the content being returned. The response is UTF-16LE so I had to remove special characters from the response to I could JSONify it.

result = result.content.replace(/[^A-Za-z0-9{}@&:\-_() /\\"=[\],.?!]/g, '')

这篇关于流星Http客户端API调用Gzip解压缩结果JSON无法正常工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆