Page 1 of 1

http request

Posted: 20 Jan 2020 13:03
by giovag7
Hi, i tried this action in Tasker and it works. I can't get it working in Automagic instead. I'm not sure how to write data in http request form. Can someone help me?
Attached is the screenshot from Tasker. How can i use that data in Automagic http request action?

Re: http request

Posted: 20 Jan 2020 17:49
by Desmanto
Use Action HTTP Request

URL : https//MY_IP_ADDRESS:8123/api/services/light/turn_on
Request Method : POST
Content Type : General Text - text/plain
Data : {"entity_id":"light.rgbw_1"}
Custom HTTP headers : tick and put
Authorization:Bearer MY_LONG_LIFE_CERTIFICATE

Put condition debug dialog after this to spot any possible error. You can check {response} value for result.

Re: http request

Posted: 20 Jan 2020 21:43
by giovag7
Hi, thank you for your reply. Tha is what I made.. But doesn't work. This js the log
Screenshot_20200120-223826.jpg
Screenshot_20200120-223826.jpg (323.58 KiB) Viewed 10789 times
It seems the connection is ok, but message is not good. Any idea? Same Body in Tasker works fine..

Re: http request

Posted: 21 Jan 2020 05:53
by Desmanto
So the only problem now is the JSON. Forgot that you can't use JSON directly there, because Automagic will Automagically parse the curly braces to a variable/expression. viewtopic.php?f=5&t=3088&p=15992&hilit= ... mat#p15992

The strange way to use it directly is to single quote it (must be single quote) and surround it by another curly braces.

Hence the Data should be

Code: Select all

{'{"entity_id":"light.rgbw_1"}'}
But I typically prepare the json before and then use function toJSON() before passing to the request. (that's why I forgot about this already). To do so, create a new map/list as your data should be. Your json is a single level map type with key-value. So it should be

Code: Select all

object = newMapFromValues("entity_id", "light.rgbw_1");
js = toJSON(object);
Then use {js} in the Data field. IMHO, this is the recommend good practice (in Automagic) to pass JSON to the http request. toJSON() will ensure any special character will be escaped properly.

Re: http request

Posted: 21 Jan 2020 10:55
by giovag7
Desmanto wrote:
21 Jan 2020 05:53
So the only problem now is the JSON. Forgot that you can't use JSON directly there, because Automagic will Automagically parse the curly braces to a variable/expression. viewtopic.php?f=5&t=3088&p=15992&hilit= ... mat#p15992

The strange way to use it directly is to single quote it (must be single quote) and surround it by another curly braces.

Hence the Data should be

Code: Select all

{'{"entity_id":"light.rgbw_1"}'}
But I typically prepare the json before and then use function toJSON() before passing to the request. (that's why I forgot about this already). To do so, create a new map/list as your data should be. Your json is a single level map type with key-value. So it should be

Code: Select all

object = newMapFromValues("entity_id", "light.rgbw_1");
js = toJSON(object);
Then use {js} in the Data field. IMHO, this is the recommend good practice (in Automagic) to pass JSON to the http request. toJSON() will ensure any special character will be escaped properly.
Thank you, works great. i used the second way. Tasker is very painful! I never wanted to use it.