0

我目前正在尝试使用节点发出 CURL 请求。

我在 PHP 中有以下 CURL 请求:-

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'https://www.website.com');
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'GET');
curl_setopt($ch, CURLOPT_POSTFIELDS,  '');
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
  'Authorization: token XXX',
  'Content-Type: application/json'
));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

$output = curl_exec($ch);

curl_close($ch);

我需要在节点中运行相同的 CURL 请求。我尝试了许多不同的 NPM 包并选择了curljs

我有以下代码: -

var curl = require("curljs");

var curlOpts = curl.opts.connect_timeout(10);

curl("https://www.website.com -H Authorization: token XXX -H Content-Type: application/json --get", curlOpts, function(err, data, stderr) {
    console.log(err);
    console.log(data);
    console.log(stderr);
});

我能够从服务器获得响应而没有任何错误,但它没有返回任何数据。我得到以下回复:-

null

  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
  0     0    0     0    0     0      0      0 --:--:-- --:--:-- --:--:--     0

有人能帮助推动正确的方向吗?

任何帮助将不胜感激。

另一个注意事项:-

我也尝试过使用请求

这是我的代码:-

var request = require('request');

var options = {
  url: 'https://www.website.com',
  headers: {
    'Authorization': 'token XXX',
    'Content-Type': 'application/json'
  }
};

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

request.get(options, callback);

但它一直返回错误。

使用node-fetch也会返回相同的错误:-

fetch("https://www.website.com", {
  method: "GET",
  headers: {
    Authorization: "token XXX",
    "Content-Type": "application/json"
  }
}).then(function(response) {
  return response;
}).then(function(response) {
  console.log(response);
});

PHP版本完美运行,我没有正确配置标题吗?

4

2 回答 2

1

HTTP 请求被烘焙到 Node.js 中。和模块是一些核心模块:它们无需您执行任何其他操作即可安装httphttps

然而,它们并不是完全易于使用。它们本身并不复杂,但它们可以更简单。

在我看来,最好的实现是fetchAPI。这包含在现代 Web 浏览器中(请参阅 MDN)。您还可以通过安装node-fetch模块在 Node 中使用它:

npm install node-fetch --save

然后,您可以使用该fetch功能拨打电话。你可以通过使用Promises.

在你的情况下:

fetch("http://www.website.com", {
  method: "GET", // not strictly necessary
  headers: {
    Authorization: "token XXX",
    "Content-Type": "application/json" // is this accurate?!
  }
}).then(function(response) {
  return response.json(); // we want JSON output
}).then(function(response) {
  console.log(response); // logs the server response
});

如果你愿意使用现代箭头函数,你可以让它更漂亮。

于 2017-01-20T20:44:06.467 回答
-1

更新

我认为您可以简单地使用request

您的场景中的示例:

var request = require('request');

var options = {
    url: 'https://www.website.com',
    headers: {
        "Authorization": "token XXX",
        "Content-Type": "application/json"
    }
};

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

request(options, callback);

该模块使用简单,并具有直观的解决方案和示例。我们在整个 Node 项目中使用了一段时间,到目前为止还没有遇到任何问题。

希望这对您有所帮助。

于 2017-04-26T01:17:34.140 回答