【发布时间】:2020-03-16 06:10:08
【问题描述】:
我有 2 部分 php cURL 代码可以正常工作。 我必须在 NODE JS 中实现。我有 2 个请求。 首先我必须登录,然后执行操作。
// login part
$this->curl = curl_init();
curl_setopt($this->curl, CURLOPT_URL, "https://my.example.com/mypage/?regid=" . username
. "&password=" . $password . "");
curl_setopt($this->curl, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($this->curl, CURLOPT_CONNECTTIMEOUT ,30);
curl_setopt($this->curl, CURLOPT_TIMEOUT, 60);
curl_setopt($this->curl, CURLOPT_COOKIEFILE, "/tmp/cookie.txt");
curl_setopt($this->curl, CURLOPT_RETURNTRANSFER, 1);
curl_exec($this->curl);
// action part
curl_setopt($this->curl, CURLOPT_URL, "https://my.example.com/mypage/action/");
curl_setopt($this->curl, CURLOPT_POST, 1);
curl_setopt($this->curl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($this->curl, CURLOPT_POSTFIELDS, $myFields);
curl_setopt($this->curl, CURLOPT_SSL_VERIFYPEER, FALSE);
$content = curl_exec($this->curl);
我能够实现第一部分,我在 console.log() 中看到了答案(正文)。
const curl = new (require( 'curl-request' ))();
curl.setHeaders([
]).setBody({
'regid': username,
'password': regpass
})
.post(registry)
.then((answer) => {
console.log('answer', answer.body);
})
.catch((e) => {
console.log(e);
});
但我无法实现第二个请求。我试着做这个例子: how to make sub HTTP request using nodejs
据我了解cookie问题,我该如何实现这部分?
【问题讨论】:
标签: node.js curl https request httprequest