获得 401 错误,尝试通过 Node.js 的 http 模块向 Heritrix 发出拆解请求。

huangapple go评论69阅读模式
英文:

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 &quot;action=teardown&quot; -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(&#39;https&#39;);

const options = {
  hostname: &#39;localhost&#39;,
  port: 8443,
  path: &#39;/engine/job/a?action=teardown&#39;,
  method: &#39;POST&#39;,
  auth: &#39;admin:admin&#39;,
  rejectUnauthorized: false
};

const req = https.request(options, res =&gt; {
  console.log(`statusCode: ${res.statusCode}`);
  
  res.on(&#39;data&#39;, d =&gt; {
    process.stdout.write(d);
  });
});

req.on(&#39;error&#39;, error =&gt; {
  console.error(error);
});

req.end();

When I run this code, I get the following console output:

statusCode: 401
&lt;html&gt;
&lt;head&gt;
   &lt;title&gt;Status page&lt;/title&gt;
&lt;/head&gt;
&lt;body style=&quot;font-family: sans-serif;&quot;&gt;
&lt;p style=&quot;font-size: 1.2em;font-weight: bold;margin: 1em 0px;&quot;&gt;Unauthorized&lt;/p&gt;
&lt;p&gt;The request requires user authentication&lt;/p&gt;
&lt;p&gt;You can get technical details &lt;a href=&quot;http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.4.2&quot;&gt;here&lt;/a&gt;.&lt;br&gt;
Please continue your visit at our &lt;a href=&quot;/&quot;&gt;home page&lt;/a&gt;.
&lt;/p&gt;
&lt;/body&gt;
&lt;/html&gt;

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,
&#39;Content-Type&#39;: &#39;application/x-www-form-urlencoded&#39;
in the request header, and reading the challenge from the WWW-Authenticate header. Then using this challenge to generate a new response.

huangapple
  • 本文由 发表于 2023年4月11日 07:05:40
  • 转载请务必保留本文链接:https://go.coder-hub.com/75981339.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定