Page 1 of 1

HTTP Post translation to Automagic

Posted: 30 Aug 2018 21:28
by Rhor
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.

Re: HTTP Post translation to Automagic

Posted: 02 Sep 2018 13:47
by Desmanto
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.

Re: HTTP Post translation to Automagic

Posted: 02 Sep 2018 14:49
by Rhor
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.

Re: HTTP Post translation to Automagic

Posted: 02 Sep 2018 17:11
by Desmanto
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.

Re: HTTP Post translation to Automagic

Posted: 12 Aug 2020 00:17
by kenvega
@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

Posted: 12 Aug 2020 01:42
by Desmanto
@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.