2017-03-10 69 views
0

所以我知道这个curl命令确实有效。当我运行它时,我得到了登录后应该出现的html文档:将curl命令转换为nodejs中的request-promise命令时遇到困难

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��% 

我在做什么错误?

回答

0

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

请注意,根据您获取数据的方式/位置,您可能会得到压缩或未压缩的响应。有关详细信息,请参阅documentation for request(在页面上搜索“compr”)。