admin管理员组文章数量:1386537
I'm sending an http request over a proxy an need to add username and password to my request. How do I correctly add these values to my options block?
This is my code:
var http = require('http');
var options = {
port: 8080,
host: 'my.proxy',
path: '/index',
headers: {
Host: ""
}
};
http.get(options, function(res) {
console.log("StatusCode: " + res.statusCode + " Message: " + res.statusMessage);
});
Currently the response is StatusCode: 307, Message: Authentication Required.
I tried to add username and password to my options but it doesn't work:
var options = {
port: 8080,
host: 'my.proxy',
username: 'myusername',
password: 'mypassword',
path: '/index',
headers: {
Host: ""
}
};
Additional Info: I don't have much information about the proxy, but in another case this authentication method worked:
npm config set proxy http://username:[email protected]:8080
I'm sending an http request over a proxy an need to add username and password to my request. How do I correctly add these values to my options block?
This is my code:
var http = require('http');
var options = {
port: 8080,
host: 'my.proxy',
path: '/index',
headers: {
Host: "http://example."
}
};
http.get(options, function(res) {
console.log("StatusCode: " + res.statusCode + " Message: " + res.statusMessage);
});
Currently the response is StatusCode: 307, Message: Authentication Required.
I tried to add username and password to my options but it doesn't work:
var options = {
port: 8080,
host: 'my.proxy',
username: 'myusername',
password: 'mypassword',
path: '/index',
headers: {
Host: "http://example."
}
};
Additional Info: I don't have much information about the proxy, but in another case this authentication method worked:
npm config set proxy http://username:[email protected]:8080
Share
Improve this question
edited May 1, 2015 at 6:33
crispychicken
asked Apr 30, 2015 at 14:41
crispychickencrispychicken
2,6622 gold badges37 silver badges51 bronze badges
1
- It really depends on the type of proxy you are going through and the type of authentication scheme it supports, do you have more information about the proxy? – robertjd Commented Apr 30, 2015 at 17:51
1 Answer
Reset to default 5 +250Ok, this works with my local squid:
var http = require('http');
function buildAuthHeader(user, pass) {
return 'Basic ' + new Buffer(user + ':' + pass).toString('base64');
}
proxy = 'localhost';
proxy_port = 3128;
host = 'www.example.';
url = 'http://www.example./index.html';
user = 'potato';
pass = 'potato';
var options = {
port: proxy_port,
host: proxy,
path: url,
headers: {
Host: host,
'Proxy-Authorization': buildAuthHeader(user, pass),
}
};
http.get(options, function(res) {
console.log("StatusCode: " + res.statusCode + " Message: " + res.statusMessage);
});
Couple notes:
- The full URL must be included in the GET line, not just the path, so its not /index.html but http://example./index.html
- You should also include the host in the host header, so you will have to parse the URL properly
本文标签: javascriptHow can I use and authorize an http proxy with nodejs httpClientStack Overflow
版权声明:本文标题:javascript - How can I use and authorize an http proxy with node.js http.Client - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744544623a2611823.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论