itgeek
itgeek

Reputation: 589

nodeJs basic authentication issue

I'm getting no proper response while make an API request to external API using basic authentication (username and password) in nodejs (javascript)

I used the below code and the response is "undefined", not sure what is missing here. But I was able to make a request using postman tool without any issues.

const request = require('request')
const user = '*****';
const pass = '*****!';

const url = 'https://servicenow.com/api/table'
var options = {
    url: url,
    auth: {
        username: user,
        password: pass
    }
};
 
request.get(options, (err, res, body) => {
    if (err) { 
        return console.log(err); 
    }
    console.log(body.url);
    console.log(body.explanation);
});

Response:

undefined
undefined

Upvotes: 1

Views: 352

Answers (1)

Mohammad Yaser Ahmadi
Mohammad Yaser Ahmadi

Reputation: 5031

if your api right with postman you can do like this based on photo

  1. send a request
  2. click on code
  3. select nodejs- Request
  4. copy

enter image description here

Upvotes: 3

Related Questions