【问题标题】:how to make sub HTTP request nodejs如何使子HTTP请求nodejs
【发布时间】: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


    【解决方案1】:

    只需在第一个回调的回调中执行第二个请求。您使用curl 有什么特别的原因吗?使用像axios 这样的直接HTTP 请求库可能更容易。这是我所知道的,所以如果你真的需要的话,你可以把它翻译成curl

    const axios = require('axios');
    
    axios({
      method: 'post',
      url: 'https://my.example.com/mypage',
      headers: {
        'X-Example-Header': 'you can send your headers here'
      }
      params: {
        regid: 'you can put your query params here and they will be added to the URL automatically'
      }
      data: {
        // this is the request body data
        regid: username,
        password: regpass
      }
    }).then(res1 => {
      // res1.data holds the response body
      // res1.headers holds the response headers
      // if the header you want contains dashes, you have to access it like this:
      // res1.headers['X-Example-Header']
      axios({
        method: 'post',
        url: 'https://my.example.com/mypage/action'
      }).then(res2 => {
        // res2.data === response of second request
      }, err => {
        console.log(err.response.data);
      });
    }, err => {
      // this function triggers if the response is a 4xx or 5xx
      // err.response.data is the response body for the error
      console.log(err.response.data);
    });
    

    希望有帮助!

    【讨论】:

    • 在第二次请求期间我们不再登录,我认为我们应该将会话保存在 cookie 中。
    • @Dav Node.js 不存储 cookie,它不是浏览器。您必须从标头中获取 cookie,然后自己将其放入变量中。
    • 兄弟,cookie 中的所有问题,我从第一个响应中获取 cookie,并在第二个请求中发送 ))) var mycookie = res1.headers['set-cookie'][0] headers: { Cookie : mycookie, 'user-agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/62.0.3202.94 Safari/537.36' },
    猜你喜欢
    • 2013-11-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-12-31
    • 2017-07-30
    • 2015-09-13
    • 1970-01-01
    相关资源
    最近更新 更多