如果有基本授权,如何在 Node.js 中使用 http.client
根据标题,我该怎么做?
这是我的代码:
var http = require('http');
// to access this url I need to put basic auth.
var client = http.createClient(80, 'www.example.com');
var request = client.request('GET', '/', {
'host': 'www.example.com'
});
request.end();
request.on('response', function (response) {
console.log('STATUS: ' + response.statusCode);
console.log('HEADERS: ' + JSON.stringify(response.headers));
response.setEncoding('utf8');
response.on('data', function (chunk) {
console.log('BODY: ' + chunk);
});
});
As per title, how do I do that?
Here is my code:
var http = require('http');
// to access this url I need to put basic auth.
var client = http.createClient(80, 'www.example.com');
var request = client.request('GET', '/', {
'host': 'www.example.com'
});
request.end();
request.on('response', function (response) {
console.log('STATUS: ' + response.statusCode);
console.log('HEADERS: ' + JSON.stringify(response.headers));
response.setEncoding('utf8');
response.on('data', function (chunk) {
console.log('BODY: ' + chunk);
});
});
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(10)
您必须在标头中设置
Authorization
字段。在本例中,它包含身份验证类型
Basic
以及以 Base64 编码的username:password
组合:You have to set the
Authorization
field in the header.It contains the authentication type
Basic
in this case and theusername:password
combination which gets encoded in Base64:来自 Node.js http.request API 文档
你可以使用类似的东西
From Node.js http.request API Docs
you could use something similar to
更简单的解决方案是直接在 URL 中使用 user:pass@host 格式。
使用 request 库:
我写了一些博客文章也与此相关。
An easier solution is to use the user:pass@host format directly in the URL.
Using the request library:
I've written a little blogpost about this as well.
就其价值而言,我在 OSX 上使用 node.js 0.6.7,但无法获得 'Authorization':auth 来与我们的代理一起使用,需要将其设置为 'Proxy-Authorization':auth
我的测试代码是:
for what it's worth I'm using node.js 0.6.7 on OSX and I couldn't get 'Authorization':auth to work with our proxy, it needed to be set to 'Proxy-Authorization':auth
my test code is:
我最近遇到了这个。
要设置的 Proxy-Authorization 和 Authorization 标头中的哪一个取决于客户端正在与之通信的服务器。
如果是Web服务器,则需要设置Authorization,如果是代理,则必须设置Proxy-Authorization标头
I came across this recently.
Which among Proxy-Authorization and Authorization headers to set depends on the server the client is talking to.
If it is a Webserver, you need to set Authorization and if it a proxy, you have to set the Proxy-Authorization header
经过大量研究后,该代码适用于我的情况。您需要安装 request npm 包。
This code works in my case, after a lot of research. You will require to install the request npm package.
对于那些不使用 DNS 且需要更多深度的用户(您也可以使用
request
代替get
,只需将get
替换为request
代码>像这样:http.request({ ... })
):For those not using DNS and needs more depth (you can also use
request
instead ofget
by simply replacingget
withrequest
like so:http.request({ ... })
):这在 Node.js 中没有详细记录,但您可以使用
因为 got pacakge 继承了它的选项对象来自http.request的
用户名
和密码
也在那里可用。This is not well documented in Node.js, but you can use
Since the got pacakge inherits it's options object from http.request,
username
andpassword
is also available there.