ioBroker / ioBroker.simple-api

HTTP-API Interface to read and write objets and states
MIT License
40 stars 19 forks source link

send API Request with Python #104

Closed stmb-100 closed 2 years ago

stmb-100 commented 2 years ago

Hi, everyone can someone please help me and tell me what I'm doing wrong. I'm trying to send an API request with Python3 to the IOBroker Simple API and get an error message back. When I call the url in the browser it works.

The log in the adapter Instance is set to "ALL" but no log entry is created. A log entry is created when sending with the browser.

import json
import requests
#       0_userdata.0.Test.Test_001

url = "http://192.168.0.83:8087"
cmd = "/get/"
point = "0_userdata.0.Test.Test_001/"
pp = "&prettyPrint"
apiurl = url + cmd + point  + pp

print(apiurl)

headers = {'Content-Type': 'text/html'}
payload={}

response = requests.request("GET", apiurl, headers=headers, data=payload)
print(response.json())

http://192.168.0.83:8087/get/0_userdata.0.Test.Test_001/&prettyPrint {'error': 'command unknown'}

R Stefan

stmb-100 commented 2 years ago

I found the bug myself

import json
import requests
#       0_userdata.0.Test.Test_001

url = "http://192.168.0.83:8087"
point = "0_userdata.0.Test.Test_001/"
cmd = "/get/"
pp = "&prettyPrint"

apiurl_1 = url + cmd + point  + pp
print(apiurl_1)

headers = {'Content-Type': 'text/html'}
payload={}

#Making http post request
response = requests.request("GET", apiurl_1, headers=headers, data=payload)
print(response.json())

cmd = "/set/"
value = "?value=700"
apiurl_2 = url + cmd + point + value  + pp
print(apiurl_2)
payload={}

response = requests.request("GET", apiurl_2, headers=headers, data=payload)
print(response.json())

response = requests.request("GET", apiurl_1, headers=headers, data=payload)
print(response.json())
Apollon77 commented 2 years ago

Please try it as the example shows it ... https://github.com/ioBroker/ioBroker.simple-api#get