Reputation: 12253
I am trying to make a WebAPI call from server script and i am getting authentication error:
This is how my server.js looks like:
var app = require('http').createServer()
, io = require('socket.io').listen(app)
, fs = require('fs')
, moment = require('moment')
, request = require('request'); //https://github.com/mikeal/request
app.listen(8000, function () {
console.log('server started');
doSomethingOnServerStart();
});
function doSomethingOnServerStart()
{
console.log('Getting something from server');
request.get({
url: 'http://localhost:63213/Api/MyAPI/GetSomething',
},
function (error, response, body) {
console.log(response.statusCode);
if (response.statusCode == 200) {
console.log('data received from server');
} else {
console.log('error: ' + response.statusCode);
console.log(body);
}
});
}
I am using Mikeal's Request library to make the WebAPI call. In the HTTP Authentication section (https://github.com/mikeal/request#http-authentication), it says pass username and password as "hash" but does not say how do i generate that hash.
I am kind of stuck and dont know how to proceed.
Upvotes: 1
Views: 2961
Reputation: 29081
To address your initial question, judging from the documentation it looks like you need to make an additional chained function call:
request.get('http://some.server.com/').auth('username', 'password', false);
or like this
request.get('http://some.server.com/', {
'auth': {
'user': 'username',
'pass': 'password',
'sendImmediately': false
}
});
In the second case it mentions having to have auth
as a hash (as it is above). It doesn't appear you're sending a username or password anywhere in your code...
Upvotes: 1