Can't connect to api website with default connection method

Python and python framework

Moderator: leecollings

Post Reply
wizzard72
Posts: 116
Joined: Friday 20 December 2013 7:45
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Can't connect to api website with default connection method

Post by wizzard72 »

I'm trying to create my first plugin. I got to manage to authenticate to the api url. I retrieve the cookie, but now I'm struggeling on how to pass the cookie to the api url. If I try it with liburl I can manage to read from the api, but not if I use the default connection method.

I got the following code:

Code: Select all

sendData = {'Verb' : 'GET',
                    'URL'  : '/api/s/default/stat/sta/',
                    'Headers' : {
                                'Cookie': self.cookie, \
                                'Host': Parameters["Address"]+":"+Parameters["Port"]
                                }
                    }
Domoticz.Log("RequestDetails sendData = "+str(sendData))
self.unifiConn.Send(sendData)
Then I got a 400 error code.

If I use liburl code:

Code: Select all

url = "https://"+Parameters["Address"]+":"+Parameters["Port"]+"/api/s/default/stat/sta/"
reqapi = urllib.request.Request(url,headers={'Cookie':self.cookie})
responseapi = urllib.request.urlopen(reqapi)
test = responseapi.read().decode('utf-8', 'ignore')
Domoticz.Log("API Response = " +test)
This works, and I got the data I want from the api.

I dont understand why it's not working with the default connection methode for plugins.
Anyone an idea?
wizzard72
Posts: 116
Joined: Friday 20 December 2013 7:45
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Re: Can't connect to api website with default connection method

Post by wizzard72 »

I changed the host header and addeds "https://" to it:

Code: Select all

sendData = {'Verb' : 'GET',
                    'URL'  : '/api/s/default/stat/sta/',
                    'Headers' : {
                                'Cookie': self.cookie, \
                                'Host': "https://"+Parameters["Address"]+":"+Parameters["Port"]
                                }
                    }
Domoticz.Log("RequestDetails sendData = "+str(sendData))
self.unifiConn.Send(sendData)
Now I got the following 400 code:

Code: Select all

{'Status': '400', 'Headers': {'Transfer-Encoding': 'chunked', 'Date': 'Fri, 20 Mar 2020 19:42:44 GMT', 'Connection': 'close'}}
Post Reply