I am trying to parse a JSON response using Node & Request. The JSON response comes from Apple, so I assume it is perfectly valid JSON. Additionally, if I use Post Master on Chrome to send the request, the raw response appears to be perfectly valid. I have even used burp suite to tap into the raw byte response - incase there is an erroneous byte in the response - again, all seems good.
However, the following code prints out complete garbage for every log statement (I have tried this on multiple machines and with different versions of Node & Request to no avail):
request(postOptions, function(error, response, body) {
if (!error && response.statusCode == 200) {
console.log(body);
console.log(body.toString());
console.log(JSON.parse(body));
}
});
Where postOptions
is:
var postOptions = {
url: '_THE_VALID_URL_',
followAllRedirects: true,
method: 'POST',
body: '_THE_VALID_BODY_',
headers: {
'Connection': 'keep-alive',
'Cache-Control': 'no-cache',
'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_2) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/40.0.2214.115 Safari/537.36',
'Accept': '*/*',
'Accept-Encoding': 'gzip, deflate',
'Accept-Language': 'en-US,en;q=0.8,pt;q=0.6',
}
}
I have made sure all the parameters are the same as the ones I am using when I make the POST request using Post Master.
What gets printed looks like:
�R�n1�k���-���V�$Pr�Ib��_Ҕ���&i��# ��{Ι���Yp��ފh��~��eNKX�b��n����|�2H�D��eD�^y���J�B��,��fk�uDj��@���P�J��ɍ��pc-pO��uaQIc���>�^hd�0%�5�<������G��#�5���m��YV�{H�� �C����o�o�N�/8�b����虬�yV�8@�f%M��ϲb\�t�S���'����M�|��2� o_� �{ÙB�#����S��*<{_�q25ؘ��sSa�^� �Frp�qn��ɾVr S%��l�.f��7ڃ��?�0���ɖ�(��P��� �~�T��U ����
Can someone please help me out here, not sure what to try next! Thanks
Thanks to @KevinB - gzipping was the issue.
By default the request module does not decompress gzipped responses. Including gzip: true
in the postOptions
ensured the response was decompressed and no more garbage!
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With