Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Request API node js using bearer token

I tried doing the following:

request({
   url: 'https://vdms-dev.clientsolve.com/evoDMDev/api_event.php',
   headers: {
      'Authorization': 'Bearer 71D50F9987529'
   }
}, function(err, res) {
       console.log(res);
});

The log is showing undefined but when I try it on Postman it seems to be working fine.

Any help would be appreciated!

like image 747
Ratri Avatar asked May 18 '18 03:05

Ratri


People also ask

How do I pass a Bearer Token in API?

Bearer tokens enable requests to authenticate using an access key, such as a JSON Web Token (JWT). The token is a text string, included in the request header. In the request Authorization tab, select Bearer Token from the Type dropdown list. In the Token field, enter your API key value.

What is Bearer Token in node JS?

This module lets you authenticate HTTP requests using bearer tokens, as specified by RFC 6750, in your Node. js applications. Bearer tokens are typically used protect API endpoints, and are often issued using OAuth 2.0.


1 Answers

Since your are calling https host (https://evodms-dev.clientsolve.com/evoDMSDev/api/api_event_all.php), request client will throws an error while doing SSL handshake, thats why you got response as undefined. Inorder to check the exact error response log the error console.error("Error Response : ", err)

Checkout this working snippet with error handling.err

Note: Now you will get Invalid Bearer Token error, Enter valid Bearer token

const request = require('request');

request({
  url: 'https://evodms-dev.clientsolve.com/evoDMSDev/api/api_event_all.php',
  headers: {
     'Authorization': 'Bearer 71D50F9987529'
  },
  rejectUnauthorized: false
}, function(err, res) {
      if(err) {
        console.error(err);
      } else {
        console.log(res.body);
      }

});
like image 142
Ashok JayaPrakash Avatar answered Sep 18 '22 16:09

Ashok JayaPrakash