cURL equivalent in Node.js?

后端 未结 18 602
误落风尘
误落风尘 2020-11-29 16:43

I\'m looking to use information from an HTTP request using Node.js (i.e. call a remote web service and echo the response to the client).

In PHP I would have used cUR

相关标签:
18条回答
  • 2020-11-29 16:59

    I ended up using the grunt-shell library.

    Here is my source gist for my fully implemented Grunt task for anyone else thinking about working with the EdgeCast API. You'll find in my example that I use a grunt-shell to execute the curl command which purges the CDN.

    This was that I ended up with after spending hours trying to get an HTTP request to work within Node. I was able to get one working in Ruby and Python, but did not meet the requirements of this project.

    0 讨论(0)
  • 2020-11-29 17:02

    Use request npm module and after call

    var request = require('request');
    request('http://www.google.com', function (error, response, body) {
      console.log('error:', error); // Print the error if one occurred
      console.log('statusCode:', response && response.statusCode); // Print the response status code if a response was received
      console.log('body:', body); // Print the HTML for the Google homepage.
    });
    

    For best practice also use some winston logger module or else simple console.log and then run your application like

    npm start output.txt 
    

    Result of above command will generate one txt file on root with all data which you have printed in console.log

    0 讨论(0)
  • 2020-11-29 17:07

    well if you really need a curl equivalent you can try node-curl

    npm install node-curl
    

    you will probably need to add libcurl4-gnutls-dev.

    0 讨论(0)
  • 2020-11-29 17:09

    I had a problem sending POST data to cloud DB from IOT RaspberryPi, but after hours I managed to get it straight.

    I used command prompt to do so.

    sudo curl --URL http://<username>.cloudant.com/<database_name> --user <api_key>:<pass_key> -X POST -H "Content-Type:application/json" --data '{"id":"123","type":"987"}'
    

    Command prompt will show the problems - wrong username/pass; bad request etc.

    --URL database/server location (I used simple free Cloudant DB) --user is the authentication part username:pass I entered through API pass -X defines what command to call (PUT,GET,POST,DELETE) -H content type - Cloudant is about document database, where JSON is used --data content itself sorted as JSON

    0 讨论(0)
  • 2020-11-29 17:10

    How about for example https://github.com/joyent/node/wiki/modules#wiki-tcp. A very quick summary =>

    • https://github.com/visionmedia/superagent
    • https://github.com/mikeal/request
    0 讨论(0)
  • 2020-11-29 17:10

    You might want to try using something like this

    curl = require('node-curl');
    curl('www.google.com', function(err) {
      console.info(this.status);
      console.info('-----');
      console.info(this.body);
      console.info('-----');
      console.info(this.info('SIZE_DOWNLOAD'));
    });
    
    0 讨论(0)
提交回复
热议问题