How do I write a Node.js request to 3rd party API?

15,339

Solution 1

Note that the answer here is a little out of date-- You'll get a deprecated warning. The 2013 equivalent might be:

app.get('/log/goal', function(req, res){
  var options = {
    host : 'www.example.com',
    path : '/api/action/param1/value1/param2/value2',
    port : 80,
    method : 'GET'
  }

  var request = http.request(options, function(response){
    var body = ""
    response.on('data', function(data) {
      body += data;
    });
    response.on('end', function() {
      res.send(JSON.parse(body));
    });
  });
  request.on('error', function(e) {
    console.log('Problem with request: ' + e.message);
  });
  request.end();
});

I would also recommend the request module if you're going to be writing a lot of these. It'll save you a lot of keystrokes in the long run!

Solution 2

Here is a quick example of accessing an external API in an express get function:

app.get('/log/goal', function(req, res){
    //Setup your client
    var client = http.createClient(80, 'http://[put the base url to the api here]');
    //Setup the request by passing the parameters in the URL (REST API)
    var request = client.request('GET', '/api/action/param1/value1/param2/value2', {"host":"[put base url here again]"});


    request.addListener("response", function(response) { //Add listener to watch for the response
        var body = "";
        response.addListener("data", function(data) { //Add listener for the actual data
            body += data; //Append all data coming from api to the body variable
        });

        response.addListener("end", function() { //When the response ends, do what you will with the data
            var response = JSON.parse(body); //In this example, I am parsing a JSON response
        });
    });
    request.end();
    res.send(response); //Print the response to the screen
});

Hope that helps!

Solution 3

This example looks pretty similar to what you are trying to achieve (pure Node.js, no express):

http://blog.tredix.com/2011/03/partly-cloudy-nodejs-and-ifs.html

HTH

Share:
15,339

Related videos on Youtube

Denis Hoctor
Author by

Denis Hoctor

A Front End Web Developer with strong experience in performance, mobile web and ASP.Net MVC. I'm also comfortable server side as I have C#, IIS and SQL knowledge that allows me to put a project live autonomously when required. I am passionate about how I approach projects and the technologies used which ensures the project is completed efficiently.

Updated on April 24, 2020

Comments

  • Denis Hoctor
    Denis Hoctor about 4 years

    Does anyone have an example of an API response being passed back from a http.request() made to a 3rd party back to my clientSever and written out to a clients browser?

    I keep getting stuck in what I'm sure is simple logic. I'm using express from reading the docs it doesn't seem to supply an abstraction for this.

    Thanks

  • user137717
    user137717 over 9 years
    how would this change with OAuth? I don't know where to put the credentials.
  • user137717
    user137717 over 9 years
    do you have a link to an explanation somewhere? i've been having trouble finding something that thoroughly explains the process.
  • Rafay
    Rafay over 6 years
    http.createClient is deprecated.

Related