Help with HTTP command with square brackets

Hi,

I’m trying to send a http command, but it has square brackets in the command. If I type it in the browser then it works fine:

http://user:[email protected]/cgi-bin/configManager.cgi?action=setConfig&VideoInMode[0].Config[0]=1

Shell command throws a URL error because of the square brackets. I tried using the --globoff command, but that doesn’t seem to send the correct command.

shell_command:
  dahua_night: 'curl --globoff -X POST "http://user:[email protected]/cgi-bin/configManager.cgi?action=setConfig&VideoInMode[0].Config[0]=1"'

I tried a restful command, but I’m getting a 401

  dahua_night:
    url: http://@192.168.1.108/cgi-bin/configManager.cgi
    method: POST
    verify_ssl: false
    username: user
    password: password
    payload: "action=setConfig&VideoInMode[0].Config[0]=1"

maybe try it with URL encoding?

I just tried it, it gives a bad hostname error

Remove the @. That’s the separator between username/password and host in browsers / curl.

Whoops! That was a typo! I’ve corrected it but still getting a 401

  dahua_night:
    url: http://192.168.1.108/cgi-bin/configManager.cgi
    method: POST
    verify_ssl: false
    username: user
    password: password
    payload: "action=setConfig&VideoInMode[0].Config[0]=1"

image

Try

dahua_night:
    url: "http://192.168.1.108/cgi-bin/configManager.cgi?action=setConfig&VideoInMode%5B0%5D.Config%5B0%5D=1"
    method: POST
    verify_ssl: false
    username: user
    password: password
    payload: ""

That still gives a 401

  dahua_night:
    url: "http://192.168.1.108/cgi-bin/configManager.cgi?action=setConfig&VideoInMode%5B0%5D.Config%5B0%5D=1"
    method: POST
    verify_ssl: false
    username: user
    password: password
    payload: ""

image

As a sanity check this is what I type into the browser. I enter user:password@192… Chrome is just not showing it

I get an OK and I can see the command has been sucessful via the device WebUI

This is the error I found in the logs

I found another post where someone was having the same issue with a command for a Dahua camera. This is now working as a shell command:

shell_command:
  dahua_day: curl -s -g --digest -u user:password "http://192.168.1.108/cgi-bin/configManager.cgi?action=setConfig&VideoInMode%5B0%5D.Config%5B0%5D=0"
  dahua_night: curl -s -g --digest -u user:password "http://192.168.1.108/cgi-bin/configManager.cgi?action=setConfig&VideoInMode%5B0%5D.Config%5B0%5D=1"

It’ll probably work with a REST command as well if you remove the method: POST I have no idea where you got from.

dahua_night:
    url: "http://192.168.1.108/cgi-bin/configManager.cgi?action=setConfig&VideoInMode%5B0%5D.Config%5B0%5D=1"
    verify_ssl: false
    username: user
    password: password

A REST command is more efficient than calling a curl inside a shell

It still gives a 401 with method: POST removed.

I found this post with an example of a REST command from a well respected member of the community:

The docs also show to use method:

A 401 Unauthorized response typically means that the device isn’t accepting your authentication. This could be for a couple of different reasons:

  • Your username or password is invalid, or isn’t being sent in the correct format (e.g. it expects Basic authentication but it’s being added somewhere else)
  • Your device is refusing to respond because Home Assistant’s origin/IP is different than your browser’s - check to see if there are any security/firewall whitelist settings on your camera.

It’s working with curl via a shell command. I’d like to know why the REST command won’t work though

Oh! Sorry, I missed that. :slight_smile:

I suspect that, under the hood, the REST component might use curl (or it may use the Python requests library, not sure), either way - it looks like it doesn’t support square brackets in the way your camera expects.

Curl’s -g flag prevents square brackets from being interpreted by curl itself, so they are passed as-is. That’s why it works.

It might be that digest basic authentication is needed, as in the curl command.

The integration doesn’t support it, unfortunately