1

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
itgeek
  • 549
  • 1
  • 15
  • 33

1 Answers1

3

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

Mohammad Yaser Ahmadi
  • 4,664
  • 3
  • 17
  • 39