1

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.

3
  • Could you make that second question an actual question. Then I'll move my answer to that question so you can accept it if it is to your liking. Let's keep questions and posts at a one-to-one on Stack Overflow :)
    – kentcdodds
    Commented Jul 9, 2013 at 20:15
  • @kentcdodds : thanks for your response. here is the link to the separate question: stackoverflow.com/questions/17558409/…
    – Asdfg
    Commented Jul 9, 2013 at 21:18
  • I hope that my answers have helped you.
    – kentcdodds
    Commented Jul 9, 2013 at 21:22

1 Answer 1

1

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...

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.