【问题标题】:NodeJS equivalent of a curl requestNodeJS 等效于 curl 请求
【发布时间】:2021-03-05 12:33:17
【问题描述】:

我正在尝试将 curl 请求转换为 nodeJS 脚本,但不知何故它在我身上失败了。通过失败的方式,我使用 cloudflare 设置的 url 会触发验证码,而当我使用 curl 请求时它不会触发我从开发工具 > 网络选项卡复制。

这是卷曲

curl -q 'https://foo.bar/path' -H 'cookie: somecookie' -H 'origin: https://foo.bar' -H 'accept-encoding: gzip, deflate, br' -H 'accept-language: en-US,en;q=0.9,pt;q=0.8' -H 'user-agent: Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/65.0.3325.181 Safari/537.36' -H 'content-type: application/x-www-form-urlencoded; charset=UTF-8' -H 'accept: */*' -H 'referer: https://foo.bar/path' -H 'authority: www.foo.bar' -H 'x-requested-with: XMLHttpRequest' --data "foo=bazz" --compressed

这里是nodeJS代码

var request = require('request');
var url = 'https://foo.bar/path';
var cookie = 'somecookie';
var ua = 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/65.0.3325.181 Safari/537.36';

var baseRequest = request.defaults({
  headers: {
    'Cookie': cookie,
    'User-Agent': ua,
    'origin': 'https://foo.bar',
    'referer': 'https://foo.bar/path'
  }
});

baseRequest.post({ url: url, formData: {
  foo: 'bazz'
}}, function(err, response, body) {
  console.log(body);
});

【问题讨论】:

  • 您可以使用网络嗅探器检查 CURL 和 node.js 请求,看看两者之间到底有什么不同。 http 请求是纯文本,所以它应该都在那里供您查看。

标签: node.js http curl request


【解决方案1】:

有一些在线工具可以为你做这件事,比如https://curl.trillworks.com/#node

顺便说一句,我为你做了,结果是:

var request = require('request');

var headers = {
    'cookie': 'somecookie',
    'origin': 'https://foo.bar',
    'accept-encoding': 'gzip, deflate, br',
    'accept-language': 'en-US,en;q=0.9,pt;q=0.8',
    'user-agent': 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/65.0.3325.181 Safari/537.36',
    'content-type': 'application/x-www-form-urlencoded; charset=UTF-8',
    'accept': '*/*',
    'referer': 'https://foo.bar/path',
    'authority': 'www.foo.bar',
    'x-requested-with': 'XMLHttpRequest'
};

var dataString = 'foo=bazz';

var options = {
    url: 'https://foo.bar/path',
    method: 'POST',
    headers: headers,
    body: dataString
};

function callback(error, response, body) {
    if (!error && response.statusCode == 200) {
        console.log(body);
    }
}

request(options, callback);

【讨论】:

  • trillworks 工具很棒!谢谢!
  • 优秀的网站。但它没有提供 -F 选项的代码。
【解决方案2】:
exec(`curl -q 'https://foo.bar/path' -H 'cookie: somecookie' -H 'origin: https://foo.bar' -H 'accept-encoding: gzip, deflate, br' -H 'accept-language: en-US,en;q=0.9,pt;q=0.8' -H 'user-agent: Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/65.0.3325.181 Safari/537.36' -H 'content-type: application/x-www-form-urlencoded; charset=UTF-8' -H 'accept: */*' -H 'referer: https://foo.bar/path' -H 'authority: www.foo.bar' -H 'x-requested-with: XMLHttpRequest' --data "foo=bazz" --compressed`
, (error, result, metadata) => {
      console.log(result);
    });

您可以使用 exec 进行直接 CURL

【讨论】:

    【解决方案3】:

    从 ES6 开始,您可以使用 async/await 来使用 curl 执行子进程。这是一个简单的例子:

    const { exec: execAsync } = require('child-process-async');
    
    const { result, stderr } = await execAsync(`curl -s https://api.ipdata.co/country_name?api-key=test`);
    console.log(result);
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-05-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多