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.
HTTP Post translation to Automagic
Moderator: Martin
Re: HTTP Post translation to Automagic
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.
Why we have so many map inside map? Well, you should ask the documentation of the smart bulbs then 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
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.
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)
))));
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}}}
}
}
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.
Xiaomi Redmi Note 5 (whyred), AOSP Extended v6.7 build 20200310 Official, Android Pie 9.0, Rooted.
Re: HTTP Post translation to Automagic
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.
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.
Re: HTTP Post translation to Automagic
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.
Xiaomi Redmi Note 5 (whyred), AOSP Extended v6.7 build 20200310 Official, Android Pie 9.0, Rooted.
Re: HTTP Post translation to Automagic
@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"
}]
}
Re: HTTP Post translation to Automagic
@kenvega : Almost similar as above, except replace the data to be
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.
Code: Select all
data = newMapFromValues(
"method", "passthrough",
"params", newList(newMapFromValues(
"deviceId", "YOUR_DEVICEID_HERE",
"otherKey", "otherValue"
))
);
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.
Xiaomi Redmi Note 5 (whyred), AOSP Extended v6.7 build 20200310 Official, Android Pie 9.0, Rooted.