57

I'm just getting started with Node, APIs, and web applications.

I understand the basic workings of Node.js and Express, but now I want to start making calls to other service's APIs and to do stuff with their data.

Can you outline basic HTTP requests and how to grab/parse the responses in Node? I'm also interested in adding specific headers to my request (initially I'm using the http://www.getharvest.com API to crunch my time sheet data).

P.S. This seems simple, but a lot of searching didn't turn up anything that answered my question. If this is dupe, let me know and I'll delete.

Thanks!

2
  • What version of Node.js are you using? Dec 15, 2011 at 6:46
  • Node version is currently 0.4.12.
    – John
    Dec 15, 2011 at 22:48

3 Answers 3

63

You cannot fetch stuff with Express, you should use Mikeal's request library for that specific purpose.

Installation: npm install request

The API for that library is very simple:

const request = require('request');
request('http://www.google.com', function (error, response, body) {
  if (!error && response.statusCode == 200) {
    console.log(body) // Print the google web page.
  }
})

Edit: You're better of using this library instead of the http default one because it has a much nicer API and some more advanced features (it even supports cookies).

UPDATE: request has been deprecated, but there are some nice alternatives still such as 'got' or 'superagent' (look them up on npm).

5
  • can you please tell me, that is it necessary to end the request, like we will be using '.end()' for the http request? Sep 2, 2015 at 15:19
  • I think that's only required for the native http client. request does that for you and exposes a friendlier interface. Sep 14, 2015 at 11:55
  • 6
    Anyone coming across this, the 'request' library is deprecated.
    – danefondo
    Aug 22, 2020 at 18:49
  • 4
    Keep in mind that this library is already depracated (since Feb 2020).
    – ulou
    Nov 2, 2020 at 14:46
  • 2
    request is now deprecated. npmjs.com/package/request
    – KIYZ
    Nov 8, 2021 at 22:55
7

You can use the http client:

var http = require('http');
var client = http.createClient(3000, 'localhost');
var request = client.request('PUT', '/users/1');
request.write("stuff");
request.end();
request.on("response", function (response) {
  // handle the response
});

Also, you can set headers as described in the api documentation:

client.request(method='GET', path, [request_headers])
1
  • 39
    In case anyone else comes across this, this method is deprecated. Sep 8, 2013 at 2:09
4

Required install two package.

npm install ejs 
npm install request

server.js

var request = require('request');
app.get('/users', function(req, res) {
    request('https://jsonplaceholder.typicode.com/users', function(error, response, body) {
        res.json(body)
    });
});

index.ejs

$.ajax({
    type: "GET",
    url: 'http://127.0.0.1:3000/posts',
    dataType: "json",
    success: function(res) {
        var res_data = JSON.parse(res);
        console.log(res_data);
    }
});

Output

enter image description here

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.