请求响应后,正文中出现奇怪的字符 [英] Weird characters in body after request response

查看:97
本文介绍了请求响应后,正文中出现奇怪的字符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用NodeJS和Request发布JSON并获取其中包含数据的结果.

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

我用Postman设置了此请求,并获得了完全可读的JSON数据.

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.
  }
}

我从Postman生成了代码片段,因此可以将其放在需要Request的NodeJS服务器上.

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

赞. 在Postman中,数据很好,但在Node中则不是.怎么会这样? 据我所知,我的请求没有错:

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

console.log提供第二个代码段中显示的数据.

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

有人可以向我解释发生了什么,为什么它可以在邮递员中工作,而不能在Node中工作?

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

推荐答案

由于发送的标头表示您愿意接受这样的响应,因此您看到的数据很可能已被压缩(使用gzip).在request()选项中设置gzip: true,并且request应该自动解压缩传递给回调的body.

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.

只需删除headers对象中的'accept-encoding': 'gzip, deflate, br'行,即可不请求gzip压缩.

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

这篇关于请求响应后,正文中出现奇怪的字符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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