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

4

15

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

Crewel answered 14/6, 2013 at 14:25 Comment(0)
A
15

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.

Adessive answered 14/6, 2013 at 14:45 Comment(9)
Thank you for the suggest but as I said in my original post, I tried this solution you can find the link at the end of my post !Crewel
Post your code using request. "I tried it and it didn't work" isn't enough for us to actually help you.Adessive
I was editing for add my code using request . thank you for your help :)Crewel
Hi sorry for late return but I tried and that's didn't work anymore :/ For more information my API is [Frapi]getfrapi.comCrewel
Most irritating thing ever: saying "it didn't work". What was the status code? Programming is a DETAIL ORIENTED endeavor. I can't help you unless you start being detail-oriented. I ran my example with 'getfrapi.com/example/1.xml' and got a 404. Can you get your request to work using curl on the command line? curl -v --digest --user test:test http://getfrapi.com/example/1.xml, for example.Adessive
Hummm sorry I said it didn't work without details because it was the same status code 401 like everytime, so I'm sorry :)Crewel
Yes that's work with curl but I'm sorry you get 404 because the URL of my API is not the one I put on stack because I didn't have the authorization maybe by MP and if I create a test account you could try but I have to ask my boss before !Crewel
Hello I asked my boss for create a test account but that's not possible so he told me to stop with Node.JS for the moment. Thank you for your help !Crewel
Example by @PeterLyons worked for me. Digest auth works great.Erdei
S
1

The digest auth in the request package seems to be incomplete.

You can try out: https://npmjs.org/package/http-digest-client ,its a pretty decent lightweight implementation for the digest authentication.

If you need to do a digest auth POST with a body message to be sent you can use request in conjunction with http-digest-client. After installing both just open up http-digest-client code under node-modules and replace its use of the http package with the request package api.

Spue answered 14/9, 2013 at 10:44 Comment(0)
K
1

Try urllib it will work with simple and disgest auth.

const httpClient = require('urllib');
const url = 'https://site.domain.com/xmlapi/xmlapi';
const options = {
  method: 'POST',
  rejectUnauthorized: false,
  // auth: "username:password" use it if you want simple auth
  digestAuth: "username:password",
  content: "Hello world. Data can be json or xml.",
  headers: {
     //'Content-Type': 'application/xml'  use it if payload is xml
     //'Content-Type': 'application/json' use it if payload is json 
    'Content-Type': 'application/text'
  }
};
const responseHandler = (err, data, res) => {
  if (err) {
    console.log(err);
  }
  console.log(res.statusCode);
  console.log(res.headers);
  console.log(data.toString('utf8'));
}
httpClient.request(url, options, responseHandler);
Kidney answered 26/7, 2019 at 13:21 Comment(0)
K
0

your sample is work

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);
Khedive answered 12/10, 2020 at 12:3 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.