Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to do request HTTP Digest auth with Node.JS?

I have to write some code with Node.JS for an API documentation, but I tried the last few days all the solutions I could found on the web (including Stack of course) without succes...

My API use HTTP Digest Auth and that's the problem, I was able to connect, that's was not a big deal but everytime I got the same return :

Got response : 401
HTTP Digest Authentication required for "api.example.com"

You can show my base code below without auth! Because I don't know what I can do after all the try I did :

var http = require('http')

var options = {
    host: 'api.example.com',
    path: '/example/1.xml',
};

var request = http.get(options, function(res){
    var body = "";
    res.on('data', function(data){
        body += data;
    })
    res.on('end', function(){
        console.log('Got response : ' + res.statusCode);
        console.log(body);
    })
    res.on('error', function(e){
        console.log('Got error : ' +e.message);
    });
});

One of my last try was to use this module https://npmjs.org/package/request but he doesn't work too as everytime I got 401 !

For more information I was able to connect and GET the information I needed from my API with Ruby, Python, php, and Java so I'm sure my API is working well and the information I pass are correct. I use the last stable of Node v0.10.11 !

If someone can help me or have a solution up to date i will be glad.

EDIT : I will add some details about my test with the module Mickael/request

First Try :

var request = require('request')

var options = {
    'url': 'http://api.example.fr/example/1.xml',
    'auth': {
        'user': 'test',
        'pass': 'test',
        'sendImmediately': false
    }
};

var request = request.get(options, function(error, response, body){
    if (!error && response.statusCode == 200){
        console.log('body : ' + body)
    }
    else{
        console.log('Code : ' + response.statusCode)
        console.log('error : ' + error)
        console.log('body : ' + body)
    }
});

Second Try :

var request = require('request')

request.get('http://api.example.fr/example/1.xml', function(error, response, body){
    if (!error && response.statusCode == 200){
        console.log('body : ' + body)
    }
    else{
        console.log('Code : ' + response.statusCode)
        console.log('error : ' + error)
        console.log('body : ' + body)
    }
}).auth('test', 'test', false);

but the return is still the same 401

like image 889
Milo Avatar asked Jun 14 '13 14:06

Milo


People also ask

How do I pass username and password authorization header node js?

Simply pass the user/pass before the host with an @ sign. var request = require('request'), username = "john", password = "1234", url = "http://" + username + ":" + password + "@www.example.com"; request( { url : url }, function (error, response, body) { // Do more stuff with 'body' here } );


1 Answers

Here's your example corrected to use request as per it's API.

var options = {
  uri: 'http://api.example.fr/example/1.xml',
  auth: {
    user: 'test',
    pass: 'test',
    sendImmediately: false
  }
};
request(options, function(error, response, body){
    if (!error && response.statusCode == 200){
        console.log('body : ' + body)
    }
    else{
        console.log('Code : ' + response.statusCode)
        console.log('error : ' + error)
        console.log('body : ' + body)
    }
});

The request chainable style API is a bit confusing (IMHO), but I believe you can make it work that way as well.

like image 86
Peter Lyons Avatar answered Sep 19 '22 16:09

Peter Lyons