Page 1 of 2

Does not work command: http://username:password@domoticz ip:port

Posted: Tuesday 23 July 2024 23:41
by reza
hi gays
i want use command http://username:password@domoticz ip:port
but does not work and when i enter this , open login page and request username and password.
also when i want send this command :
http://username:[email protected]: ... itchcmd=On
i have answer:
401 Unauthorized

Re: Does not work command: http://username:password@domoticz ip:port

Posted: Wednesday 24 July 2024 0:06
by waltervl
Do you have setting "Allow Basic-Auth authentication over plain HTTP (API only)" enabled in menu setup - Settings, tab security?

Edit: also some browsers do not allow to add user:password so to test you have to use something like curl or postman.

Re: Does not work command: http://username:password@domoticz ip:port

Posted: Wednesday 24 July 2024 0:22
by mark.sellwood
I have also been trying to backup from a curl script with http://username:password@ipaddress/backupdatabase.php and get the same 401 Unauthorized
I have "Allow basic authorisation for HTTP" enabled in setup.
I know the username / password are good as I use them to log in

Re: Does not work command: http://username:password@domoticz ip:port

Posted: Wednesday 24 July 2024 0:27
by waltervl
mark.sellwood wrote: Wednesday 24 July 2024 0:22 I know the username / password are good as I use them to log in
And that user is an admin? Else it has no right to make a backup.
Also if you login from an IP in the trusted network range don't give user:password.

Re: Does not work command: http://username:password@domoticz ip:port

Posted: Wednesday 24 July 2024 0:44
by mark.sellwood
Yes, the user is an Admin
If I add the Raspberry IP in as a Trusted Networks it works fine with no user:password

Re: Does not work command: http://username:password@domoticz ip:port

Posted: Wednesday 24 July 2024 2:08
by reza
waltervl wrote: Wednesday 24 July 2024 0:06 Do you have setting "Allow Basic-Auth authentication over plain HTTP (API only)" enabled in menu setup - Settings, tab security?

Edit: also some browsers do not allow to add user:password so to test you have to use something like curl or postman.

edit : i resolve it with curl. thank you

Re: Does not work command: http://username:password@domoticz ip:port

Posted: Wednesday 24 July 2024 8:39
by mark.sellwood
Could you please let me know the command you are using?
This is waht I gave & am getting the error/
/usr/bin/curl -k "https://user:password@IP_Address/backupdatabase.php" > /home/Mark/tmp/Backup.db

Re: Does not work command: http://username:password@domoticz ip:port

Posted: Wednesday 24 July 2024 11:36
by FlyingDomotic
Are there any ":", "@" or "/" in username and/or password?

Re: Does not work command: http://username:password@domoticz ip:port

Posted: Wednesday 24 July 2024 13:21
by mark.sellwood
No, just standard text

Re: Does not work command: http://username:password@domoticz ip:port

Posted: Wednesday 24 July 2024 13:28
by FlyingDomotic
mark.sellwood wrote: Wednesday 24 July 2024 8:39 Could you please let me know the command you are using?
This is waht I gave & am getting the error/
/usr/bin/curl -k "https://user:password@IP_Address/backupdatabase.php" > /home/Mark/tmp/Backup.db
It seems you're using https in curl, while using http in first message. Is this an error ?

Re: Does not work command: http://username:password@domoticz ip:port

Posted: Wednesday 24 July 2024 13:32
by mark.sellwood
My bad, this is what im using
/usr/bin/curl -s "http://user:password@IP_Address:8080/backupdatabase.php" > /home/Mark/tmp/Backup.db

Re: Does not work command: http://username:password@domoticz ip:port

Posted: Wednesday 24 July 2024 14:16
by FlyingDomotic
What's exact error message?

I assume you properly checked user and password, including lower/uppercase.

Re: Does not work command: http://username:password@domoticz ip:port

Posted: Wednesday 24 July 2024 14:27
by mark.sellwood
In the output file I have
<html><head><title>Unauthorized</title></head><body><h1>401 Unauthorized</h1></body></html>

Re: Does not work command: http://username:password@domoticz ip:port

Posted: Wednesday 24 July 2024 14:40
by waltervl
For backup issue also see viewtopic.php?p=315818

Re: Does not work command: http://username:password@domoticz ip:port

Posted: Wednesday 24 July 2024 15:15
by FlyingDomotic
I found something...

I just tried the same (curl -k "http://username:password@ip:8080/backupdatabase.php" > Backup.db) with 2024.6 and 2020.2 with success.

I also tried without username and password (curl -k "http://ip:8080/backupdatabase.php" > Backup.db) and it also worked.

Both tests where done with the requesting machine in list of trusted networks in Security tab of Settings.

When removing it from list of trusted networks, I got the same error as you, with and without password (401 errors)

When trying API call (/json.htm?type=command&param=getversion), everything ran well.

Going back to wiki (RTFM), if found at https://www.domoticz.com/wiki/Security:

Code: Select all

Provide valid credentials via 'Basic-Auth' with each API request. Only accepted for API calls!
As your call is not an API call (not starting with /json.htm), you're not allowed to identify this way. You should either:
  • Put machine (or subnet) into trusted network list
  • As doc says: Retrieve an 'Access Token' (JWT Token) from the IAM server and provide this Bearer token with each request
First method worked for me, and I never tried second one...

Tip: carefully check that trusted networks are separated by a ";" as I had some surprises with a mistyped ","

Re: Does not work command: http://username:password@domoticz ip:port

Posted: Wednesday 24 July 2024 19:06
by mark.sellwood
Thanks for checking that our for me, I was coming to the same conclusion
ie to do a backup the User needs to be an Admin & the IP needs to be Trusted.

Re: Does not work command: http://username:password@domoticz ip:port

Posted: Wednesday 24 July 2024 22:35
by FlyingDomotic
... or write a small script, on machine having Domoticz, to generate backup locally and copy database to remote machine.

Re: Does not work command: http://username:password@domoticz ip:port

Posted: Wednesday 24 July 2024 23:43
by waltervl
Or just enable the built in backup mechanism with automated hourly, daily and monthly backups.

Re: Does not work command: http://username:password@domoticz ip:port

Posted: Saturday 27 July 2024 19:37
by reza
mark.sellwood wrote: Wednesday 24 July 2024 8:39 Could you please let me know the command you are using?
This is waht I gave & am getting the error/
/usr/bin/curl -k "https://user:password@IP_Address/backupdatabase.php" > /home/Mark/tmp/Backup.db
in a .py file i use this code:

Code: Select all

import pycurl
from io import BytesIO
buffer = BytesIO()
curl = pycurl.Curl()
curl.setopt(pycurl.URL, 'http://username:[email protected]:8080/json.htm?type=command&param=switchlight&idx=1&switchcmd=On')
curl.setopt(pycurl.USERAGENT, "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:120.0) Gecko/20100101 Firefox/120.0")
curl.setopt(pycurl.WRITEDATA, buffer)
curl.perform()
curl.close()

Re: Does not work command: http://username:password@domoticz ip:port

Posted: Thursday 22 August 2024 19:18
by mark.sellwood
Thanks for all your feedback.
I have now enabled the automated backups & once a day I copy the off to my NAS as a off line backup.