How to execute HTTP Digest request using Node.JS?

I need to write code with Node.JS for API documentation, but I tried the last few days all the solutions that I could find on the Internet (including Stack, of course) without succes ...

My API uses Auth HTTP Digest and that the problem I was able to connect to was not a big problem, but every time I got the same income:

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

You can show my base code below without authorization! Because I do not know what I can do after all attempts:

 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 attempts was to use this module https://npmjs.org/package/request , but it does not work, since every time I received 401!

For more information, I was able to connect and get the information I need from my API with Ruby, Python, php, and Java, so I'm sure my API works well and the information I'm passing is correct. I use the latest stable 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 using the Mickael / request module

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 attempt:

 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

+9
source share
3 answers

Here your example is fixed to use request in accordance with its 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 programmed API is somewhat confusing (IMHO), but I believe you can make it work the same way.

+11
source

Authorization in the request package seems incomplete.

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

If you need to run the auth POST command with a body message to send, you can use the request in conjunction with http-digest-client. After installation, just open the http-digest-client code under the node modules and replace its use of the http package with the request api package.

+2
source

Try urllib , it will work with simple authentication.

 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); 
0
source

All Articles