3

I'm trying to create a backend that sends a GET request to another API.

For example:

My API: localhost:3000/
Route: /getdata/data1
Other API: api.com/target/data

(This is a fake URL, just assume that this route has the data that I want)

How can I send a get request to that API from my API? Ajax.get?

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
Ext-
  • 471
  • 3
  • 8
  • 16
  • The other API is in the same web application? – Chetan Jun 25 '18 at 00:26
  • Different. Its another webapp. I essentialy want my api to request the other api’s data – Ext- Jun 25 '18 at 00:28
  • And the response from my api is = response from other api (json data) – Ext- Jun 25 '18 at 00:32
  • Possible duplicate of [How do I make calls to a REST api using c#?](https://stackoverflow.com/questions/9620278/how-do-i-make-calls-to-a-rest-api-using-c) – Chetan Jun 25 '18 at 00:33
  • https://www.rapiddg.com/blog/calling-rest-api-nodejs-script – Chetan Jun 25 '18 at 00:36
  • The easiest way is to install a module like [axios](https://www.npmjs.com/package/axios) and use it to make the requests. – Mark Jun 25 '18 at 00:57

2 Answers2

2

You can use the built-in http module in node, or use a third-party package such as request.

HTTP

An example of using the built in http module e.g:

// The 'https' module can also be used
const http = require('http');

// Example route
app.get('/some/api/endpoint',  (req, res) => {

    http.get('http://someapi.com/api/endpoint', (resp) => {
        let data = '';

        // Concatinate each chunk of data
        resp.on('data', (chunk) => {
          data += chunk;
        });

        // Once the response has finished, do something with the result
        resp.on('end', () => {
          res.json(JSON.parse(data));
        });

        // If an error occured, return the error to the user
      }).on("error", (err) => {
        res.json("Error: " + err.message);
      });
});

Request

Alternatively, a third party package such as request can be used.

First install request:

npm install -s request

And then change your route to something like this:

const request = require('request');

// Example route
app.get('/some/api/endpoint',  (req, res) => {

    request('http://someapi.com/api/endpoint',  (error, response, body) => {
        if(error) {
            // If there is an error, tell the user 
            res.send('An erorr occured')
        }
        // Otherwise do something with the API data and send a response
        else {
            res.send(body)
        }
    });
});
Ash
  • 6,483
  • 7
  • 29
  • 37
0

For Node.js, use request.

Example:

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

Replace http://www.google.com with your URL. You'll need to see if you need to authorize with the other API; otherwise you will get HTTP 401.

Matt Kuhns
  • 1,328
  • 1
  • 13
  • 26