Jelmer Jelmer - 4 months ago 13
JSON Question

Weird characters in body after request response

I am using NodeJS and Request to post JSON and get a result with data in it.

I set this request up with Postman and I get the JSON data back which is completely readable.

{
"d": {
"__type": "Qvision.WoningenModule.Lib.aanbodcollection",
"testOutput": "",
"filter": null,
...
etc.
}
}


I generated the code snippet from Postman so I could put it on my NodeJS server with Request required.

When I try to run the request on my node server a encoded body.

��`I�%&/m�{J�J��t�`$ؐ@�������iG#)�*��eVe]f@�흼��{����{����;�N'���?\fdl��J�ɞ!���?~|?"~�G�����~�߿�^�=��'.������V�by�/��f�2?/&�,[N�ٴ*�|�R��F�y�~�nW��^��ϋ����-�eI�Vm���Z�������|�ѣ��"���b�}�h�|+4���~QWU����>������Ge6�ˆ�_��"����^o��bF�HO<��GM[gYK���E�\/���G[��UN^��eN��͊�Ң��A_?�n���Ƙ˼Z�������f@��z�Q b=)��Zѯ��|�Fo+z�.~m.�z��墚�%��̫�uMs�y1�x�uA����>���Ų��/�fZ��h����"����Y����͐Ƣ@w���w����5+s;�����1��2�$��G��?������Ų�b�O�ˬ����9���~P-gy�L��|i/�����Q��$_��H�E��_�|Y���� <s8��/��8�����$��?~��C�J#{�3������o6ķ{�wSU4����YK���j�L�D�K�D��K�kan '$����uQ��ĺ�]�� ��J4Z������O������O?������O~�'��~��d?�N�{���G��{;����ݽ����*��}j���O�@�N�^=�ڊ>+�Dƍ�]%QM�wjy���������!�)� ��������g���?�&����R����i�|�6��N�U�v��o�> ���;����`��?xpp��"f��������;�v��U�sДy~V�����w�o�m~�:#�������x�Ӄ��ß������`g��A�$�����>�G�z����b���ߧ$�;�;�_�Mȼ�lv�`��po�vxNB{Y�WP?�����{{�j��t���g�˽����C�>1j��G jE���?���Ux�/ww���b2�DI���ݧ��aK� k5���}n]���|�tgw��:�;;���J���S�O��>�O� �/���\x�=��?��/�>B����O���;��:ݻ�G��9�%�v<�h䱶^:��{|�KF����{�����o�4����.�M�<�=��w�����I����+M��>i�{�ݽ��=�|oߨb3ޟ�.����v�����j��n�z|x��*1u?�~JF����à������c&}mФK�س���f���t���&ELF���e��>�Y��?�.fUճ�;��oh����oЪ�?��#��si�)Z����9������~��/>������Ҍ��}���KR�Cf����=��� 6�$3������Y5�~����vw�Hż�lvHf|�������Rh�s�T��gnj����ݿo{�Y1�ԉ�o���{�`g_�kv̸��>y��>4���ϒ�;}@����l�Ϛ���O*l�S#�1ey����uЌ�����'�ۆ��o�ʤu�"җ��O~�,�{�����[Xo1��FQ����t���F�sk���h�|��d������ �=��٦���>�)F��=ܷif3���.)�J)_�~VͶ��>E��ΰ�9�Y�����`��N��f��{� �ώ��z�Oi����턺���O?}`����1�~���;v6a=�̶�)-���6��7g�7tI���}�T���Mf������*ofy��ZbY��.�B��M���g�l� 3�?ͷ;�"���n�@_|sv{��� ���ݦo����ߟ�@�m\"�w����o?=��,��s;�v��7��Y�r�!%���c��Y���[R7����h�ڭX���)�}������ޣ���~�p�ٱ�~�(ͱc��d�g��������[1��ٱ�~�H�K��h��NHT��u��5��w������e���ɢ�LZt�)=s~��C^�v������Ɓ!K� �����F�x�c՟W-�I���U4�5Ɠl88 �ױ�;����ᅬx������5I�R84%�J�s��|�<���ʚS��ӇdQ��O����W�� �g�GR{�~�=���r�ׇ�0����UL��bv��������&��>%"�^v ��)��},�h�?+v��n����33����q�?Z8~H|���gɊ{}�k���C#�?kF������c��� ��^6�W��9,2�]������0�5���X.�D�0��&;�SD(�������ڈ��� 5F�����1�?��<�j��#���Ů�QψS�m����|�%������/�_�<~�����9�e^^��nה�� �
�ϕ)���ރO�?��G�~�v�v�/3���i���������g�WZP}p���p��g�?ʑ����DT����l�#���{fWz��1�^�~�{��X����r��]
�vMw�ώ)�����?�p`2 0�?K�������m�?k��둖�|�gz�i���σ�| ���J����/YF��R�G?k��}b���#$߿1C~g� �C�C��� ���/� ��%K�9


Like this.
In Postman the data is fine but in Node it isn't. How can this be?
There is nothing wrong with my request as far as I know:

var request = require("request");

var options = { method: 'POST',
url: '****',
headers:
{ 'postman-token': '****',
cookie: '****',
'accept-language': 'nl-NL,nl;q=0.8,en-US;q=0.6,en;q=0.4',
'accept-encoding': 'gzip, deflate, br',
referer: '****',
'content-type': 'application/json; charset=UTF-8',
'user-agent': 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/51.0.2704.103 Safari/537.36',
'x-requested-with': 'XMLHttpRequest',
origin: '****',
accept: 'application/json, text/javascript, */*; q=0.01',
'cache-control': 'no-cache',
pragma: 'no-cache',
'content-length': '1035',
connection: 'keep-alive',
host: '****' },
body: '{"****************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************}',
//encoding: null
};

request(options, function (error, response, body) {
if (error) throw new Error(error);
console.log(body);
});


The console.log gives the data shown in the 2nd code snippet.

Can someone explain to me what's going on and why this works in postman but not in Node?

Answer

Most likely the data you're seeing is compressed (with gzip) since you are sending a header that says you are willing to accept such a response. Set gzip: true in your request() options and request should automatically decompress the body passed to your callback.

Or just remove the 'accept-encoding': 'gzip, deflate, br' line in your headers object to not request gzip compression.

Comments