【问题标题】:Trouble converting a curl command to a request-promise command in nodejs在nodejs中将curl命令转换为request-promise命令时遇到问题
【发布时间】:2017-03-10 22:07:01
【问题描述】:

所以我知道这个 curl 命令确实有效。当我运行它时,我得到了登录后应该出现的 html 文档:

curl 'https://login.url' 
-H 'Pragma: no-cache' 
-H 'Origin: https://blah' 
-H 'Accept-Encoding: gzip, deflate, br' 
-H 'Accept-Language: en-US,en;q=0.8' 
-H 'Upgrade-Insecure-Requests: 1' 
-H 'User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_11_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/56.0.2924.76 Safari/537.36' 
-H 'Content-Type: application/x-www-form-urlencoded' 
-H 'Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8' 
-H 'Cache-Control: no-cache' 
-H 'Referer: https://blash.jsp' 
-H 'Connection: keep-alive' 
--data 'Username=mary&Password=<PASSWORD>&Go=Login&Action=Srh-1-1' --compressed ;

这是我尝试将其转换为节点请求承诺。当我运行它时,我得到了一些奇怪的字符。

var url = 'https://login.url';

var headers = {
  'Pragma': 'no-cache',
  'Origin': 'https://blah',
  'Accept-Encoding': 'gzip, deflate, br',
  'Accept-Language': 'en-US,en;q=0.8',
  'Upgrade-Insecure-Requests': '1',
  'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_11_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/56.0.2924.76 Safari/537.36',
  'Content-Type': 'application/x-www-form-urlencoded',
  'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8',
  'Cache-Control': 'no-cache',
  'Referer': 'https://blash.jsp',
  'Connection': 'keep-alive',
}

var data = {
  'Username':'mary',
  'Password':'<Password>',
  'Go':'Login',
  'Action':'Srh-1-1'
}

var html = yield request.post({url:url,form:data,headers:headers});

这是一个奇怪字符的例子:

�X}o�6����4\��b�N��%

我做错了什么?

【问题讨论】:

    标签: node.js curl


    【解决方案1】:

    您需要通过将gzip 选项设置为true 来告诉request 您接受压缩。

    请注意,根据您获取数据的方式/位置,您可能会获得压缩或未压缩的响应。详情请查看documentation for request(在页面上搜索“compr”)。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-08-23
      • 2019-06-04
      • 1970-01-01
      • 1970-01-01
      • 2016-06-08
      • 2018-02-15
      • 2018-06-15
      • 2014-12-04
      相关资源
      最近更新 更多