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
4
  • What do you get when you log err, res and body ? Commented Feb 13, 2021 at 20:56
  • it did not print those values Commented Feb 13, 2021 at 21:12
  • It logged undefined for all three ? Commented Feb 13, 2021 at 21:13
  • can i know what is the correct method / syntax to make a request in javascript ? Commented Feb 13, 2021 at 21:13

1 Answer 1

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

Sign up to request clarification or add additional context in comments.

1 Comment

with some tweaking, this worked for me

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.