我遇到了麻烦,当在execSync中使用node.js时,它不能直接在shell中输入命令。
以下是我的问题:
我使用一个curl从服务器请求一些数据,我需要使用cookie来实现这一点,因为需要登录。
很容易处理登录过程并获得cookie,但奇怪的是,在curl中使用cookie会导致服务器出现“内部错误”。而且,由于我没有更改服务器代码的权限,所以我希望了解在Node.js中调用Node.js并直接使用curl的不同之处。
以下是代码:
var command = 'curl --cookie cookie.txt ' + getURL();
console.log(command);
// output: curl --cookie cookie.txt http://example.com/getdata
var result = child_process.execSync(command).toString();
// will cause an internal error and the "result" is an error-reporting page.在shell中直接调用它:
curl --cookie cookie.txt http://example.com/getdata一切正常,我得到了我需要的数据。
我试图找到一些情节,例如,将代码更改为:
var command = 'curl --cookie cookie-bad.txt ' + getURL();我在cookie-bad.txt中放了一些错误的曲奇,我会得到一个“你没有登录”的结果。
所以一定有什么问题:
向服务器发送cookie,以请求一些数据,其中运行curl,运行在带有execSync的nodejs脚本中。
有什么办法可以改进代码吗?
发布于 2019-10-19 13:15:15
你的Node.js版本是什么?我对10.16.0没有任何问题。

https://stackoverflow.com/questions/58462555
复制相似问题