I am trying to access an API using "request" npm. This API requires header "content-type" and a basic authentication. here is what I've done so far.
var request = require('request');
var options = {
url: 'https://XXX/index.php?/api/V2/get_case/2',
headers: {
'content-type': 'application/json'
},
};
request.get(options, function(error, response, body){
console.log(body);
}
).auth("[email protected]","password",false);
upon executing this using Node , I am getting an error that says invalid username and password. I've validated the same API, authentication and header using CURL with the command below and it gave an expected HTTP response.
curl -X GET -H "content-type: application/json" -u [email protected]:password "https://XXX/index.php?/api/V2/get_case/2"
Please suggest the right way to code request with auth and header.
Here is my update code
var auth = new Buffer("[email protected]" + ':' + "password").toString('base64');
var req = {
host: 'https://URL',
path: 'index.php?/api/v2/get_case/2',
method: 'GET',
headers: {
Authorization: 'Basic ' + auth,
'Content-Type': 'application/json'
}
};
request(req,callback);
function callback(error, response, body) {
console.log(body);
}
I am seeing 'undefined' in my console. can you help me here?
Here is how it worked for me
var auth = new Buffer(user + ':' + pass).toString('base64');
var req = {
host: 'https://XXX/index.php?/api/V2/get_case/2',
path: path,
method: 'GET',
headers: {
Authorization: 'Basic ' + auth,
'Content-Type': 'application/json'
}
};
request(url, {
method: "POST",
auth: {
user: this.user,
pass: this.pass
}
}, function (error, response, body) {
if (!error && response.statusCode == 200) {
console.log('body:', body);
} else {
console.log('error', error, response && response.statusCode);
}
});
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With