Meteor Http Client API Call Gzip uncompress result

2019-07-13 17:29发布

问题:

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.

Here's my code:

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);

The error is as per the screenshot:

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

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.

回答1:

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