Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to make a external API call inside express server?

Tags:

Hello I ve been trying to implement OneSignal API on my dashboard and I wonder if it is possible to make a API external call inside express server.

Here is an example:

var sendNotification = function(data) {   var headers = {     "Content-Type": "application/json; charset=utf-8",     "Authorization": "Basic NGEwMGZmMjItY2NkNy0xMWUzLTk5ZDUtMDAwYzI5NDBlNjJj"   };    var options = {     host: "onesignal.com",     port: 443,     path: "/api/v1/notifications",     method: "POST",     headers: headers   };    var https = require('https');   var req = https.request(options, function(res) {       res.on('data', function(data) {       console.log("Response:");       console.log(JSON.parse(data));     });   });    req.on('error', function(e) {     console.log("ERROR:");     console.log(e);   });    req.write(JSON.stringify(data));   req.end(); }; 

Here it is the app route

app.post('/path', function(req, res){   var message = {    app_id: "5eb5a37e-b458-11e3-ac11-000c2940e62c",   contents: {"en": "English Message"},   included_segments: ["All"] };  sendNotification(message); }); 

Thank you!

like image 527
enrico_alvarenga Avatar asked Oct 24 '16 15:10

enrico_alvarenga


People also ask

How can make API call from Express server?

Making REST API Calls In Express. Create a new route in express called /getAPIResponse. This route will make a REST API call and return the response as JSON. You'll be making use of the request client to make REST API calls in express.

Can one API call another API?

Like telephones, API calls enable APIs to talk to one another and exchange information.


2 Answers

I wonder if it is possible to make a API external call inside express server.

Sure, you can contact any external server from a node.js app with http.request() like you are showing or one of the higher level modules built on top of that like the request module.

Here's a simple example from the request module home page:

const request = require('request'); request('http://www.google.com', function (error, response, body) {   if (!error && response.statusCode == 200) {     console.log(body) // Show the HTML for the Google homepage.    } }); 

Or, using promises:

 const rp = require('request-promise');  rp('http://www.google.com').then(body => {      console.log(body);  }).catch(err => {      console.log(err);  }); 

EDIT Jan, 2020 - request() module in maintenance mode

FYI, the request module and its derivatives like request-promise are now in maintenance mode and will not be actively developed to add new features. You can read more about the reasoning here. There is a list of alternatives in this table with some discussion of each one. I have been using got() myself and it's built from the beginning to use promises and is simple to use.

like image 192
jfriend00 Avatar answered Sep 18 '22 15:09

jfriend00


you can use Axios client as Axios is a Promise based HTTP client for the browser as well as node.js.

Using Promises is a great advantage when dealing with code that requires a more complicated chain of events. Writing asynchronous code can get confusing, and Promises are one of several solutions to this problem.

First install Axios in your application using npm install axios --save

and then you can use this code

const axios = require('axios');  axios.get('api-url')     .then(response => {         console.log(response.data.status);         // console.log(response.data);         res.send(response.data.status);     })     .catch(error => {         console.log(error);     }); 
like image 25
GUDDU KUMAR Avatar answered Sep 21 '22 15:09

GUDDU KUMAR