dahua icon indicating copy to clipboard operation
dahua copied to clipboard

Unable to Retrieve Snapshot from Dahua DVR API - 401 Unauthorized

Open roni9843 opened this issue 8 months ago • 0 comments

Dahua DVR Snapshot Issue

Description

I am trying to capture a snapshot from my Dahua DVR using a Node.js project, but I am encountering a 401 Unauthorized error despite using the correct credentials.

Project Details

I am using Node.js with Axios to send requests to the Dahua DVR API. The goal is to retrieve a snapshot from the DVR.

API Endpoint

http://admin:[email protected]/cgi-bin/snapshot.cgi?channel=1

When accessed via a browser, the URL prompts for credentials and works as expected. However, in my Node.js application, it returns a 401 Unauthorized error.

Current Behavior

Receiving a 401 Unauthorized error when sending a GET request to the API using Axios in Node.js.

Node.js Code

const axios = require('axios');
const fs = require('fs');

const url = "http://admin:[email protected]/cgi-bin/snapshot.cgi?channel=1";

axios({
  method: 'get',
  url: url,
  responseType: 'stream',
  auth: {
    username: 'admin',
    password: 'abc12345'
  }
})
.then(response => {
  response.data.pipe(fs.createWriteStream('snapshot.jpg'));
  console.log("Snapshot saved as snapshot.jpg");
})
.catch(error => {
  console.log("Error:", error.message);
});


Troubleshooting Steps Taken

  • Verified credentials by logging in via the browser.
  • Ensured that the DVR’s HTTP Authentication settings are correctly configured.
  • Attempted using both Basic Authentication and Digest Authentication.
  • Checked the Security settings on the DVR but could not find specific HTTP Authentication options.
  • Verified that the API endpoint works directly in the browser but fails in Node.js.

Expected Behavior

The API should return a snapshot image without authentication errors when accessed via Node.js.

Image

Image

Image

Image

URL working on my Chrome but Node js not Working

Image

this is my node js error

roni9843 avatar Feb 03 '25 07:02 roni9843