http request

Post your questions and help other users.

Moderator: Martin

Post Reply
User avatar
giovag7
Posts: 58
Joined: 05 Mar 2013 10:02

http request

Post by giovag7 » 20 Jan 2020 13:03

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?
Attachments
photo_2020-01-20_14-03-26.jpg
photo_2020-01-20_14-03-26.jpg (52.21 KiB) Viewed 10679 times

User avatar
Desmanto
Posts: 2709
Joined: 21 Jul 2017 17:50

Re: http request

Post by Desmanto » 20 Jan 2020 17:49

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.
Index of Automagic useful thread List of my other useful posts (and others')
Xiaomi Redmi Note 5 (whyred), AOSP Extended v6.7 build 20200310 Official, Android Pie 9.0, Rooted.

User avatar
giovag7
Posts: 58
Joined: 05 Mar 2013 10:02

Re: http request

Post by giovag7 » 20 Jan 2020 21:43

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 10653 times
It seems the connection is ok, but message is not good. Any idea? Same Body in Tasker works fine..

User avatar
Desmanto
Posts: 2709
Joined: 21 Jul 2017 17:50

Re: http request

Post by Desmanto » 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.
Index of Automagic useful thread List of my other useful posts (and others')
Xiaomi Redmi Note 5 (whyred), AOSP Extended v6.7 build 20200310 Official, Android Pie 9.0, Rooted.

User avatar
giovag7
Posts: 58
Joined: 05 Mar 2013 10:02

Re: http request

Post by giovag7 » 21 Jan 2020 10:55

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.

Post Reply