Send POST from script

Hi. It is possible to call a script that posts a url.
Here I have a code but something is wrong.
The name of the script is send_medition and is called by pressing on “GRABAR MEDICION”

Thanks

Untitled

script.yaml

send_medition:
  url: http://localhost
  method: POST
  headers: 
    authorization: 'Bearer ABCDEFGH'
    content-type: 'application/json'
  payload: '{"state":entity???}'

The REST sensor that can use the POST method. If you can get that to work you can use homeassistant.update_entity to force the sensor to run.


Another option would be to create a shell command. Here is an example I found. This example assumes you have access to curl on your operating system.

curl -d '{"key1":"value1", "key2":"value2"}' -H "Content-Type: application/json" -X POST http://localhost:3000/data

I also see there is a notification rest method.

Thanks. I will try

Hi. Everything is working but I can’t pass the entities. How do I send it? Thanks

shell_command:
  send_medition: 'curl -X GET http://localhost/?action=setMeditions&valor={{input_text.valor_medicion}}&quimico={{input_select.quimicos}}'

Try to copy paste the shell command in the template editor and see if it returns the expected results.

This is de error

Detalles de registro (ERROR)
Sun Dec 22 2019 18:55:23 GMT-0300 (hora estándar de Argentina)
Error rendering command template: UndefinedError: 'input_text' is undefined
Traceback (most recent call last):
  File "C:\Users\Mile\AppData\Local\Programs\Python\Python37\lib\site-packages\homeassistant\helpers\template.py", line 222, in async_render
    return compiled.render(kwargs).strip()
  File "C:\Users\Mile\AppData\Local\Programs\Python\Python37\lib\site-packages\jinja2\asyncsupport.py", line 76, in render
    return original_render(self, *args, **kwargs)
  File "C:\Users\Mile\AppData\Local\Programs\Python\Python37\lib\site-packages\jinja2\environment.py", line 1008, in render
    return self.environment.handle_exception(exc_info, True)
  File "C:\Users\Mile\AppData\Local\Programs\Python\Python37\lib\site-packages\jinja2\environment.py", line 780, in handle_exception
    reraise(exc_type, exc_value, tb)
  File "C:\Users\Mile\AppData\Local\Programs\Python\Python37\lib\site-packages\jinja2\_compat.py", line 37, in reraise
    raise value.with_traceback(tb)
  File "<template>", line 1, in top-level template code
  File "C:\Users\Mile\AppData\Local\Programs\Python\Python37\lib\site-packages\jinja2\sandbox.py", line 387, in getattr
    value = getattr(obj, attribute)
jinja2.exceptions.UndefinedError: 'input_text' is undefined

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "C:\Users\Mile\AppData\Local\Programs\Python\Python37\lib\site-packages\homeassistant\components\shell_command\__init__.py", line 46, in async_service_handler
    rendered_args = args_compiled.async_render(service.data)
  File "C:\Users\Mile\AppData\Local\Programs\Python\Python37\lib\site-packages\homeassistant\helpers\template.py", line 224, in async_render
    raise TemplateError(err)
homeassistant.exceptions.TemplateError: UndefinedError: 'input_text' is undefined
curl -v -X POST http://localhost -H "Content-type:application/json" -d '{"name":"pablo"}'

if I try this returns nothing comes by POST
Array ()

C:\Users\Mile>curl -v -X POST http://localhost -H "Content-type:application/json" -d '{"name":"pablo"}'
Note: Unnecessary use of -X or --request, POST is already inferred.
* Rebuilt URL to: http://localhost/
*   Trying ::1...
* TCP_NODELAY set
* Connected to localhost (::1) port 80 (#0)
> POST / HTTP/1.1
> Host: localhost
> User-Agent: curl/7.55.1
> Accept: */*
> Content-type:application/json
> Content-Length: 14
>
* upload completely sent off: 14 out of 14 bytes
< HTTP/1.1 200 OK
< Date: Sun, 22 Dec 2019 23:32:56 GMT
< Server: Apache/2.4.41 (Win64) OpenSSL/1.0.2s PHP/7.1.33
< X-Powered-By: PHP/7.1.33
< Set-Cookie: PHPSESSID=a9pdhe71r4jooq2l3aq4k0vo6o; path=/
< Expires: Thu, 19 Nov 1981 08:52:00 GMT
< Cache-Control: no-store, no-cache, must-revalidate
< Pragma: no-cache
< Content-Length: 184
< Content-Type: application/json