HASS rest_command

Hello, I have tested

rest_cammand:
  dsh_switch_on:
    name: 'DSH rest function'
    url: http://192.168.3.125/HA/rest.php
    method: POST
    headers:
      accept: "application/json, text/html"
      user-agent: 'Mozilla/5.0 {{ useragent }}'
    content_type:  'application/json; charset=utf-8'
    payload: '{"action":"status", "status":"{{status}}"}'

switch:
  - platform: rest
    name: 'Test Rest function'
    resource: http://192.168.3.125/HA/rest.php
    body_on: '{"status":"true", "action":"status"}'
    body_off: '{"status":"false", "action":"status"}'
    is_on_template: "{{ value_json.is_active }}"
    headers:
      Content-Type: application/json

I follow to:

I have small php code that accept and write to a log file all incomes data.

{"method":"GET","payload":[],"action":null,"time":"16:58:31","ip":"192.168.3.125","status":""}

Unfortunately I can’t receive/get payload. Both of methods call php without payload. Even the method aren’t same as I call from HA.
Can somebody help me with it?

If you want to receive something you want a rest sensor:

command, plus what tom_l said.

Hello tom_I, I need to send control commands from HA to a device. I had sensor by REST that receive commands.

Is your PHP valid? Seems strange that it would print"GET" for “POST”.
Maybe show it…

<?php 
//echo"Begin<br>";
$method = $_SERVER['REQUEST_METHOD'];
$answ['method'] = $method;
//if($method == 'POST'){
$answ['payload'] = $_REQUEST;
if(isset($_POST['action'])) $act = $_POST['action'];
if(isset($_GET['action'])) $act = $_GET['action'];
//echo"Mthod: $method<br>";
$status = "";
if(isset($_POST['status'])) $status = $_POST['status'];
$answ['action'] = $act;
$answ['time'] = date("G:i:s");
$answ['ip'] = $_SERVER['REMOTE_ADDR'];
$answ['status'] = $status;
file_put_contents('m_log.log', json_encode($answ));

//echo"Action: $act<br>";
$answ = [];
$ini = parse_ini_file('config.ini');
$db_file = $ini['db_file'];
$fs = filesize($db_file);
//echo"<br>File $db_file has $fs bytes<br>"; 
if($fs == null){
    $answ['answer'] = 'ok';
    $answ['dev'] = 'rest_simulator';
    $answ['data'] = 'something';
    
    $answ['state'] = 'on';
    $answ['status'] = 'off';
    $answ['time'] = date('H:i:s');
    file_put_contents($db_file, json_encode($answ));
}
else{
    $string_data = file_get_contents($db_file);
    $answ = json_decode($string_data, true);
    $answ['method'] = $method;
    $answ['payload'] = $_REQUEST;
    file_put_contents($db_file, json_encode($answ));
}
if($act == "status"){
    $st = "";
    if($method == 'POST') $st = $_POST['status'];
    else $st = $_GET['status'];
    $answ['status'] = $st;
    file_put_contents($db_file, json_encode($answ));
//    echo"<br>Action 'status' = ".$answ['status']."<br>";
}
$answ['is_active'] = "true";

echo json_encode($answ);

?>

The type GET/POST doesn’t matter cause I put into $answ (array) all data from $_REQUEST.
And as you can see from log (above) - the income request has method - GET, instead of documentation that I had use to make request in HA. Anyway I put into answer all data from request: $answ['payload'] = $_REQUEST;
Any other ideas?

PHP only supports POST with application/x-www-form-urlencoded content type.
$_POST is empty with application/json

Use, e.g.:

<?php
//echo"Begin<br>";
$method = $_SERVER['REQUEST_METHOD'];
$answ['method'] = $method;
$payload = "";
if($method == 'POST'){
    $inputJSON = file_get_contents('php://input');
    $payload = json_decode($inputJSON, TRUE); 
}
$answ['payload'] = $payload;
if(isset($payload['action'])) $act = $payload['action'];
if(isset($_GET['action'])) $act = $_GET['action'];
//echo"Mthod: $method<br>";
$status = "";
if(isset($payloadT['status'])) $status = $payload['status'];
$answ['action'] = $act;
$answ['time'] = date("G:i:s");
$answ['ip'] = $_SERVER['REMOTE_ADDR'];
$answ['status'] = $status;
file_put_contents('m_log.log', json_encode($answ));

//echo"Action: $act<br>";
$answ = [];
$db_file = 'test.txt';;
$fs = filesize($db_file);
//echo"<br>File $db_file has $fs bytes<br>";
if($fs == null){
    $answ['answer'] = 'ok';
    $answ['dev'] = 'rest_simulator';
    $answ['data'] = 'something';

    $answ['state'] = 'on';
    $answ['status'] = 'off';
    $answ['time'] = date('H:i:s');
    file_put_contents($db_file, json_encode($answ));
}
else{
    $string_data = file_get_contents($db_file);
    $answ = json_decode($string_data, true);
    $answ['method'] = $method;
    $answ['payload'] = $payload;
    file_put_contents($db_file, json_encode($answ));
}
if($act == "status"){
    $st = "";
    if($method == 'POST') $st = $payload['status'];
    else $st = $_GET['status'];
    $answ['status'] = $st;
    file_put_contents($db_file, json_encode($answ));
//    echo"<br>Action 'status' = ".$answ['status']."<br>";
}
$answ['is_active'] = "true";

echo json_encode($answ);
?>

EDIT: Obviously, with a correct rest_command, as the others said

rest_command:
  dsh_switch_on:
    url: http://cloud.pax:8000/rest.php
    method: POST
    headers:
      accept: "application/json, text/html"
    content_type:  'application/json; charset=utf-8'
    payload: '{"action":"status", "status":"my status"}'

First, this command appears in services list, by calling this service HA send request, but without content/payload (I have tried both methods - PUT/POST):

rest_command:
  dsh_rest_switch_on:
    #name: 'DSH rest function'
    url: http://192.168.3.125/HA/rest.php
    method: put
    headers:
        accept: "application/json, text/html"
        user-agent: 'Mozilla/5.0 {{ useragent }}'
    content_type:  'application/json; charset=utf-8'
    payload: '{"action":"status", "status":"my_status"}'

Second, this code:

switch:
  - platform: rest
    name: 'Test Rest function'
    resource: http://192.168.3.125/HA/rest.php
    #?action=status&status=on
    method: put
    body_on: '{"status":"true", "action":"status"}'
    body_off: '{"status":"false", "action":"status"}'
    is_on_template: "{{ value_json.is_active }}"
    headers:
        Content-Type: application/json

doesn’t send content/payload (I have tried put/post - both doesn’t works)
when i use hard code :

resource: http://192.168.3.125/HA/rest.php?action=status&status=on

then command/payload - action=status&status=on - comes.
This switch also send back value_json.is_active that accept and recognise in HA.
So, today result:

  1. rest_command - appears in services section of developer tools
  2. changing method in rest_command POST comes to php as GET, method PUT comes as PUT
  3. payload from rest_command doesn’t comes into php
  4. switch platform
    a) doesn’t send content/payload
    b) receive answer from php and parsing well by HA
    c) hard-coding GET request works

Have you any ideas about it?

p.s.: I have implemented your code in php:

$answ['request_body'] = $_REQUEST;

if($method == 'POST'){
    $inputJSON = file_get_contents('php://input');
    $payload = json_decode($inputJSON, TRUE);
}
$answ['payload'] = $payload;

So I can see payload as you offer and raw - $_REQUEST

{"method":"GET","request_body":[],"payload":null,"action":null,"time":"5:20:51","ip":"192.168.3.125","status":""}

Additional. I have tested several ideas - try to change this code:

rest_command:
  dsh_rest_switch_on:
    url: http://192.168.3.125/HA/rest.php
    method: POST
    headers:
        accept: "application/json, text/html"
    content_type:  "application/x-www-form-urlencoded"
    payload: "mode=off"
#  '{"action":"status", "status":"my_status"}'
  1. changing method on POST/GET/PUT doesn’t reflect on income in php method, it always detect as GET in $_SERVER[‘REQUEST_METHOD’]
  2. the payload doesn’t appear in php
    Examples from: ‘RESTful Command - Home Assistant

Not really the proper forum to debug your php code.

First debug the php by using curl. When it works as you want it, translate the curl commands in HA, then come back with working curl and not working config in HA if applicable.

Hello koying, my php code here to show how I test and get results. The problem aren’t in php. I use php just because it easy to make a Tests. But you didn’t undertand a task and matter of my question. I have sensor (emulated with php) that can send data to HASS - it works. Now I test sending data from HASS to … device (I also use php to receive data from HASS because it easy and faster to me).
So, could you be so kind to check my notes and tell me - what’s wrong or better way.

P.S.: I can use command line to send data from HA, but it seems to me much better use REST commands.

Well, the one you posted wasn’t working.
There might be other bugs in it that you’d want to debug via curl or postman first before integrating in HA.

Finally, my test gives me results:

  1. Method ‘post’ doesn’t works, in this mode HASS send GET request with null body.
  2. Method ‘put’ works as GET and send JSON array but as a plain text. (Usually we send ‘?action=my_action&status=true’ - and it can be parsed by standard methods).
    My config.yaml
switch:
  - platform: rest
    name: 'Test Rest function'
    resource: http://192.168.3.183/func
    #?action=status&status=on
    method: put
    body_on: '{"status":"true", "action":"status"}'
    body_off: '{"status":"false", "action":"status"}'
    is_on_template: "{{ value_json.status }}"
    # value_template: "{{ value_json.status }}"
    headers:
        Content-Type: application/json
    #content_type:  'application/json; charset=utf-8'
    #payload: '{"action":"status", "status":"my status"}'

I leave a commented strings to show that it’s not working.
Now I can receive and parse income from HA data, my switch in HA also receive the state of the load (on/off).
Thank to everybody who had spend a time to answer me.

One more comment to this problem.
There are two messages that comes - one post request with payload and then get with empty body.
My mistake was that I logging messages and last message over cover first, mean that I always saw last request - GET with Empty Body.
Finally I find a way - decoding method then if ‘empty get’ then send back a status, if ‘post’ then decode as normal way - parsing and check arguments as usual.
Also here are an asking request comes every 30-60 seconds with ‘Empty GET’ that ask a state.
Sorry for my misunderstanding.