Wednesday, June 20, 2018

Meteor Http Client API Call Gzip uncompress resultant JSON not working

Leave a Comment

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://atlas.atdw-online.com.au/api/atlas/products', {     params: {       key: '594c18ea1a184997a5457593b5873c8f',       att: 'INDIGENOUSAUTHARTS',       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: Returned content is weird in result.content

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 Answers

Answers 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, '') 
If You Enjoyed This, Take 5 Seconds To Share It

0 comments:

Post a Comment