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.

Asdfg
  • 11,362
  • 24
  • 98
  • 175
  • 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 Jul 09 '13 at 20:15
  • @kentcdodds : thanks for your response. here is the link to the separate question: http://stackoverflow.com/questions/17558409/how-do-i-avoid-stroing-username-password-when-using-request-module-with-node-js – Asdfg Jul 09 '13 at 21:18
  • I hope that my answers have helped you. – kentcdodds Jul 09 '13 at 21:22

1 Answers1

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

kentcdodds
  • 27,113
  • 32
  • 108
  • 187