I am trying to make an Electron project that interfaces with the VAPIX to send PTZ move commands. Before I start sending the move commands, I am trying to make the ping test work. The command is as follows: https://192.168.30.10/axis-cgi/pingtest.cgi?ip=192.168.30.10
When I send this request in a chrome browser, I have to input the username and password and then the page shows got response.
I can also send this command like this: https://user:pass@192.168.30.10/axis-cgi/pingtest.cgi?ip=192.168.30.10
which bypasses the manual authentication and then the page shows 'got response'.
My electron project is a node.js based application that Electron complies into a chrome window. When I click a button it runs the following code:
const axios = require('axios');
const https = require('https');
// command is https://192.168.30.10/axis-cgi/pingtest.cgi?ip=192.168.30.10
async function sendTestCommand( event, command ) {
const username = 'root';
const password = 'password';
const agent = new https.Agent({
rejectUnauthorized: false // This bypasses SSL certificate validation.
});
const basicAuth = 'Basic ' + Buffer.from( username + ':' + password ).toString( 'base64' );
const config = {
httpsAgent: agent,
headers: {
'Authorization': basicAuth,
}
};
console.log( 'Headers:', config.headers );
try {
const response = await axios.get( command, config );
console.log( 'Response:', response.data );
return response.data;
} catch ( error ) {
console.error( 'Error:', error.response ? error.response.data : error.message );
return { error: true, message: error.message };
}
}
I am getting Request failed with status code 401
in the program
Then the console prints this:
Error: <!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN">
<html><head>
<title>401 Unauthorized</title>
</head><body>
<h1>Unauthorized</h1>
<p>This server could not verify that you
are authorized to access the document
requested. Either you supplied the wrong
credentials (e.g., bad password), or your
browser doesn't understand how to supply
the credentials required.</p>
</body></html>
I also try to change the code to have no authentication and just send the command with the user:pass@
in the URL but it also returns the same message.
I have now also tried to use a digest form of authentication, here is the code for that:
const AxiosDigest = require('axios-digest').default;
const https = require('https');
// command is https://192.168.30.10/axis-cgi/pingtest.cgi?ip={}}
async function sendTestCommand(event, command) {
const username = 'root';
const password = 'password';
const httpsAgent = new https.Agent({
rejectUnauthorized: false,
});
const axiosDigest = new AxiosDigest(username, password);
console.log(axiosDigest.info);
axiosDigest.post( command, {}, {httpsAgent} )
.then(response => {
console.log(response.data);
})
.catch(error => {
console.error('Request failed', error);
});
}
This code returns this:
{ username: 'root', passwd: '***' }
Request failed Error: Auth params error.
at AxiosDigest.getAuthHeader (C:\Users\Administrator\Documents\ElectronCameraOverlay\node_modules\axios-digest\index.js:177:19)
at AxiosDigest.<anonymous> (C:\Users\Administrator\Documents\ElectronCameraOverlay\node_modules\axios-digest\index.js:89:34)
at step (C:\Users\Administrator\Documents\ElectronCameraOverlay\node_modules\axios-digest\index.js:33:23)
at Object.throw (C:\Users\Administrator\Documents\ElectronCameraOverlay\node_modules\axios-digest\index.js:14:53)
at rejected (C:\Users\Administrator\Documents\ElectronCameraOverlay\node_modules\axios-digest\index.js:6:65)
at process.processTicksAndRejections (node:internal/process/task_queues:95:5)
Make sure in your camera settings authentication policy set to 'Basic' since you are trying to connect with Basic Auth. By default axis camera auth policy must be set to 'Digest' at least mine came with default to 'Digest'. Depends on the model, Axis cameras may support both Basic and Digest auth policy at the same time but you need to set it in the camera settings under network auth policy. I noticed that you're using https. Did you install SSL to your camera? If not, try http.
Try this
const AxiosDigest = require('axios-digest').default;
const https = require('https');
// command is https://192.168.30.10/axis-cgi/pingtest.cgi?ip=192.168.30.10}
async function sendTestCommand(event, command) {
const username = 'root';
const password = 'password';
const agent = new https.Agent({
rejectUnauthorized: false,
});
const axiosDigest = new AxiosDigest({
username,
password,
httpsAgent: agent,
});
console.log(axiosDigest.info);
try {
const response = await axiosDigest.post(command, {});
console.log(response.data);
} catch (error) {
console.error('Request failed', error);
}
}