Jump to content
  • 0

[SOLVED]REST API syntax for POST and PUT


Question

Hi to all,

 

Sorry if I started a new discussion but I don't find information about my doubt.:(

 

My main need is to send some information from an external server to HC2 ... to a scena ... or a virtual device...

 

I try to call HC2 through API from another server using REST API but i cannot find the correct syntax to ... for examples "press a button " in a virtual devices...

 

There is someone that can help me?

 

 

Thanks a lot in advance 

Paolino 

 

 

 

Link to post
Share on other sites

5 answers to this question

Recommended Posts

  • 0

Thanks a lot for upper answer, 

but i don't understand how I can build the "body" when I use the PUT o POST options...where I can find the syntax ?

 

for example, if I want to push a button in a virtual device ... I need to invoke:

 

... /devices/{deviceID}/action/{actionName} and fill the body with the name of the button... 

 

my problem is to found the syntax for the body...

 

in the official documentation I don't find this information... :(

 

thanks 

Paolino

Link to post
Share on other sites
  • 0
5 minutes ago, Paolino65 said:

but i don't understand how I can build the "body" when I use the PUT o POST options...where I can find the syntax ?

 

auth=('[email protected]', 'Micro$oft')

def do_request(path, method="GET", params={}, headers={}):
        if method == "GET":
            response = requests.get(path, headers=headers, auth=auth)
        elif method == "POST":
            headers["Content-Type"] = "application/json"
            response = requests.post(
                path,
                data=json.dumps(params),
                headers=headers,
                 auth=auth)
        elif method == "PUT":
            headers["Content-Type"] = "application/json"
            response = requests.put(
                path,
                data=json.dumps(params),
                headers=headers,
                 auth=auth)
        elif method == "DELETE":
            response = requests.delete(
                path,
                data=json.dumps(params),
                headers=headers,
                auth=auth)
        return response


def set_global_value(code, value):
	try:
		data = {"name": code, "value": str(value)}
                r = do_request('http://192.168.1.36/api/globalVariables/'+code, "PUT", data)
		#print(r)
		if r.status_code==200:
			data = r.json()
			return data 
		else:
			return None
	except requests.exceptions.RequestException as e:  # This is the correct syntax
		print('FIBARO ERROR! Exception {}'.format(e))
		return None
	except: # catch *all* exceptions
		e = sys.exc_info()[0]
		print("FIBARO Unexpected error:", e)
		return None

      set_global_value("KatrinPresence", 1)

python example

Link to post
Share on other sites
  • 0

Thanks a lot!

 

i need to find the content of the body in order to press the button (id=1) of the virtual device 483..

 

http://192.168.1.203:80/api/devices/483/action/pressButton

 

in the body I should be declare the button id... but I don't know the correct Json ...

 

thank a lot in advance

Paolino

Link to post
Share on other sites
  • 0

I am sorry, but press button call not use POSt or PUT http methods

 

http://<HC2 ip address>/api/callAction?deviceID=15&name=pressButton&arg1=2 

 

Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Answer this question...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...