Search code examples
phpnode.jscurlnpmcurljs

Convert PHP CURL to NPM CURL


I am currently attempting to make a CURL request using node.

I have the following CURL request in PHP:-

$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);

I need to run this same CURL request in node. I have attempted many different NPM packages and settled on curljs.

I have the following code:-

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);
});

I am able to get a response from the server without any errors, but it is not returning any data. I get the following response:-

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

Would anybody be able to help with a push in the right direction?

Any help would be greatly appreciated.

On another note:-

I have also tried to use request

Here is my code:-

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);

But it keeps returning an error.

Using node-fetch also returns the same error:-

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);
});

The PHP version works perfectly, am I not configuring the headers correctly?


Solution

  • HTTP requests are baked into Node.js. The http and https modules are some of the core modules: they are installed without you having to do anything else.

    They aren't, however, totally simple to use. They're not complex per se, but they could be simpler.

    The nicest implementation in my mind is the fetch API. This is included in modern web browsers (see MDN). You can also use it in Node by installing the node-fetch module:

    npm install node-fetch --save
    

    You can then make your call using the fetch function. You get the results, in a nice, modern ES6-ish kind of way, by using Promises.

    In your case:

    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
    });
    

    You can make this even prettier if you are willing to use modern arrow functions.