如何使用Node.JS请求HTTP Digest身份验证? [英] How to do request HTTP Digest auth with Node.JS?
问题描述
我必须使用Node.JS编写一些代码以获取API文档,但最近几天我尝试了在网上找到的所有解决方案(当然包括Stack),但没有成功...
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...
我的API使用HTTP Digest Auth,这就是问题所在,我能够连接,这没什么大不了的,但是每次我得到相同的回报时:
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);
});
});
我最后的尝试之一是使用此模块 https://npmjs.org/package/request但是他每次我都收到401时都不工作!
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 !
有关更多信息,我能够使用Ruby,Python,php和Java从API连接并获取所需的信息,因此,我确定我的API可以正常工作,并且传递的信息是正确的.我使用的是Node v0.10.11的最后一个稳定版!
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.
我将使用模块 Mickael/request
第一次尝试:
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)
}
});
第二次尝试:
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);
但是返回值仍然是401
but the return is still the same 401
推荐答案
在此示例中,您的示例已更正为根据其API使用 request
.
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)
}
});
请求链接样式API有点令人困惑(IMHO),但我相信您也可以使它工作.
The request chainable style API is a bit confusing (IMHO), but I believe you can make it work that way as well.
这篇关于如何使用Node.JS请求HTTP Digest身份验证?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!