英文:
Getting 401 error when trying to make a teardown request to Heritrix via Node.js http module
问题
我正在尝试通过Node.js的http模块和Heritrix REST API发出拆除请求,但我一直收到401错误。我知道使用curl可以成功发出请求,因为我已经使用以下命令进行了测试:
curl -v -d "action=teardown" -k -u admin:admin --anyauth --location https://localhost:8443/engine/job/a
然而,当我尝试使用Node.js的http模块复制这个命令时,我收到未授权的错误。以下是我正在使用的代码:
const https = require('https');
const options = {
hostname: 'localhost',
port: 8443,
path: '/engine/job/a?action=teardown',
method: 'POST',
auth: 'admin:admin',
rejectUnauthorized: false
};
const req = https.request(options, res => {
console.log(`statusCode: ${res.statusCode}`);
res.on('data', d => {
process.stdout.write(d);
});
});
req.on('error', error => {
console.error(error);
});
req.end();
当我运行这段代码时,我得到以下控制台输出:
statusCode: 401
<html>
<head>
<title>Status page</title>
</head>
<body style="font-family: sans-serif;">
<p style="font-size: 1.2em;font-weight: bold;margin: 1em 0px;">Unauthorized</p>
<p>The request requires user authentication</p>
<p>You can get technical details <a href="http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.4.2">here</a>.<br>
Please continue your visit at our <a href="/">home page</a>.
</p>
</body>
</html>
我不确定我做错了什么。您有关于如何让这个工作的建议吗?
英文:
I'm trying to make a teardown request to Heritrix via Node.js http module and the Heritrix REST API, but I keep getting a 401 error. I know the request works using curl, as I've tested it with the following command:
curl -v -d "action=teardown" -k -u admin:admin --anyauth --location https://localhost:8443/engine/job/a
However, when I attempt to replicate this command with the Node.js http module, I get an error saying that I am unauthorized. Here is the code I am using:
const https = require('https');
const options = {
hostname: 'localhost',
port: 8443,
path: '/engine/job/a?action=teardown',
method: 'POST',
auth: 'admin:admin',
rejectUnauthorized: false
};
const req = https.request(options, res => {
console.log(`statusCode: ${res.statusCode}`);
res.on('data', d => {
process.stdout.write(d);
});
});
req.on('error', error => {
console.error(error);
});
req.end();
When I run this code, I get the following console output:
statusCode: 401
<html>
<head>
<title>Status page</title>
</head>
<body style="font-family: sans-serif;">
<p style="font-size: 1.2em;font-weight: bold;margin: 1em 0px;">Unauthorized</p>
<p>The request requires user authentication</p>
<p>You can get technical details <a href="http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.4.2">here</a>.<br>
Please continue your visit at our <a href="/">home page</a>.
</p>
</body>
</html>
I'm not sure what I'm doing wrong. Any suggestions on how I can get this to work?
答案1
得分: 0
我对这里的401错误了解不多。解决方法是向服务器发送一个不带任何身份验证的请求,'Content-Type': 'application/x-www-form-urlencoded'
在请求头中,并从WWW-Authenticate头中读取挑战,然后使用这个挑战生成一个新的响应。
英文:
I had little understanding of 401 errors here. The solution to this would be to request an auth challenge from the server by sending a request with no authentication whatsoever,
'Content-Type': 'application/x-www-form-urlencoded'
in the request header, and reading the challenge from the WWW-Authenticate header. Then using this challenge to generate a new response.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论