HTTP Post translation to Automagic

Post your questions and help other users.

Moderator: Martin

Post Reply
Rhor
Posts: 5
Joined: 03 Jun 2017 18:00

HTTP Post translation to Automagic

Post by Rhor » 30 Aug 2018 21:28

Hello.

I am trying to make a HTTP POST request from Automagic in order to control smart bulbs and plugs.

The commands are the following:

curl --request POST "https://eu-wap.tplinkcloud.com/?token=YOUR_TOKEN_HERE HTTP/1.1" \
--data '{"method":"passthrough", "params": {"deviceId": "YOUR_DEVICEID_HERE", "requestData": "{\"system\":{\"set_relay_state\":{\"state\":1}}}" }}' \
--header "Content-Type: application/json"

I have followed some tutorials and I can make it work from "Insomnia RES Client" in Windows (https://insomnia.rest/)

How can I translate those commands into Automagic?
I am trying the action HTTP Request POST but since I am ignorant about these kinds of things, I really do not know what to put in what position.

Any help would be greatly appreciated.

Thank you.

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

Re: HTTP Post translation to Automagic

Post by Desmanto » 02 Sep 2018 13:47

While you can put the whole json in the data field, I would prefer using Map object, as you can modify the key/value directly in automagic. You can look at the similar case here : viewtopic.php?f=5&t=3088
So the first step it to populate the needed json in a map object, use action script before the HTTP Post.

Code: Select all

data = newMapFromValues(
"method", "passthrough",
"params", newMapFromValues(
"deviceId", "YOUR_DEVICEID_HERE",
"requestData", newMapFromValues(
"system", newMapFromValues(
"set_relay_state", newMapFromValues("state", 1)
))));
Why we have so many map inside map? Well, you should ask the documentation of the smart bulbs then :lol: I just follow and translate it to automagicish


When you use debug dialog to see the value of data, tap and show in JSON format, you will see something like this

Code: Select all

{
  "method": "passthrough",
  "params": {
    "deviceId": "YOUR_DEVICEID_HERE",
    "requestData": {"system": {"set_relay_state": {"state": 1}}}
  }
}
You can then use Action HTTP request
URL : https://eu-wap.tplinkcloud.com/?token=YOUR_TOKEN_HERE HTTP/1.1
Request Method : POST
Content Type : General Text
application/json
Data : {data,jsonformat}

I am not so sure actually for the content Type, but it seems to be the only choice then. You can try Content Type : Form, if the General Text doesn't work.
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.

Rhor
Posts: 5
Joined: 03 Jun 2017 18:00

Re: HTTP Post translation to Automagic

Post by Rhor » 02 Sep 2018 14:49

Thank you for the very detailed answer.
I was able to "translate" the original cURL command to use POST instead using a plugin called HTTP Shortcuts which works on Automagic as well.

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

Re: HTTP Post translation to Automagic

Post by Desmanto » 02 Sep 2018 17:11

If we have built-in action to do it already, I suggest to just use built-in one. It will save the footprint (you don't need extra app) and reduce power consumption as well.
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.

kenvega
Posts: 12
Joined: 19 Nov 2019 17:57

Re: HTTP Post translation to Automagic

Post by kenvega » 12 Aug 2020 00:17

@Desmanto how would it work if we would need an array here? my data to send to other API needs one like this:

Code: Select all

{
  "method": "passthrough",
  "params": [{
    "deviceId": "YOUR_DEVICEID_HERE",
    "otherKey": "otherValue"
  }]
}

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

Re: HTTP Post translation to Automagic

Post by Desmanto » 12 Aug 2020 01:42

@kenvega : Almost similar as above, except replace the data to be

Code: Select all

data = newMapFromValues(
"method", "passthrough",
"params", newList(newMapFromValues(
  "deviceId", "YOUR_DEVICEID_HERE",
  "otherKey", "otherValue"
  ))
);
As you can see the pattern, curly braces in json is replaced to newMapFromValues() and bracker replaced to newList().

Using debug dialog and show data in JSON format, it should show the same json result as your post above.
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.

Post Reply