Rest Post request to endpoint from HASS - Help with Syntax?

Hi All,

I’m trying to use home assistant to connect to an endpoint rest service. I have the rest command working in Postman, but am a little shaky on the Syntax within Home Assistant.

Here is an example in Python:

Thanks, here’s an example of my post in python:

import requests

url = "https://api.particle.io/v1/devices/1e0026001247343339383037/WARM_STRIP/"

payload = "access_token=SECRET&args=%221%2C0%2C10%22"
headers = {
    'content-type': "application/x-www-form-urlencoded",
    'cache-control': "no-cache"
    }

response = requests.request("POST", url, data=payload, headers=headers)

print(response.text)

I’m struggling to convert this to HASS format. Also, I would like to be able to use a home assistant variable for the “args” parameter.

Any help would be greatly appreciated.

Best,
Rob

Alternatively, here is an example of the request using cURL:

curl -X POST -H "Content-Type: application/x-www-form-urlencoded" -H "Cache-Control: no-cache" -d 'access_token=SECRET&args="1%2C0%2C10"' "https://api.particle.io/v1/devices/1e0026001247343339383037/WARM_STRIP/"

I’m quite the noob here, but just if it helps here’s a working rest entry from my sensors.yaml:

  - platform: rest
    resource: http://192.168.178.30:5000/mote/api/v1.0/channel/all/state
    method: GET
    name: Mote Square Circle LED States
    value_template: '{{ value_json.state | dictsort }}'

So I’m guessing, this would at least let you see what HASS makes of it:

  - platform: rest
    resource: https://api.particle.io/v1/devices/1e0026001247343339383037/WARM_STRIP/
    method: GET
    name: Particle Devices
    payload: '{ "access_token" }'
    value_template: '{{ value_json.state | dictsort }}'

The last two lines I would leave out until you’ve seen what it gives you, but as an example they might prove useful later.

Thanks, still trying to understand this. It’s a post request with both an access token and arguments, both go in the payload?

So maybe like:

  - platform: rest
    resource: https://api.particle.io/v1/devices/1e0026001247343339383037/WARM_STRIP/
    method: POST
    name: Particle Devices
    payload: '{ "access_token: SECRET, args: "1,100,30"" }'
    value_template: '{{ value_json.state | dictsort }}'

Can I have quotes within quotes? The arguments have to be passed as a complete string of comma separated values.

I was able to make some progress with the help of a fellow forum member:

switch 2:
  - platform: rest
    name: 'Particle Devices'
    resource: https://api.particle.io/v1/devices/1e0026001247343339383037/WARM_STRIP/?access_token=SECRET
    method: POST
    body_on: "{'args': '1,100,10'}"

The switch above does send a post to the device, however, the arguments are not being passed correctly. the format is typically something like args=“1,100,10” where args are passed as a string.

I have also tried “args=1,100,10” and “1,100,10” without luck. In a cURL request the arguments are in quotes and passed as a string args=“1%2C0%2C10”’ How can I use quotes within quotes?

I think the problem is that the Content-Type header is not sent.
Experimenting with Postman (chrome extension) I found that I can post both JSON and args=value but I have to set the correct content type header, i.e. application/json or application/x-www-form-urlencoded. Without the header, the request doesn’t work.

I think the solution now would be to be able to set the content type header in the REST command config.

Hey I just used your format to post some Plex information into a HASS html sensor, and it worked perfectly - so thanks! I suspect that mixing your quotes in the headers section might not help; as all I did was I make the quote symbols consistent, and added the HASS auth type to the headers e.g. ‘x-ha-access’: ‘hasspasswrd’