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