Search code examples
javascriptnode.jsauthorizationheritrix

Getting 401 error when trying to make a teardown request to Heritrix via Node.js http module


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?


Solution

  • 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.