-1

im trying try to POST a request in a BASH script but somehow, my json data cant be find bellow is my code.

header=$(curl  -sb -H "Accept: application/json" "http://localhost:4040/api/tunnels")

# this is json format
tunnels=`echo "$header"`


echo "{'token':'xoxp-token-key','channel':'hybridteam','appname':'Server Unit', 'data': ${tunnels}}"

header2=$(curl -H "Content-type: application/json" -H "Accept: application/json" -sb -d "{'token':'token-key-here','channel':'hybridteam-ngrok','appname':'This is testing PC', 'data': ${tunnels}}" -X POST  "https://localhost:8080")

success=`echo "$header2"`
echo $success

And my Server gets the data like this

$data = json_decode(isset($_POST['data'])? $_POST['data'] : $_GET['data']);
$token = isset($_POST['token'])? $_POST['token'] : $_GET['token'];
$channel = isset($_POST['channel'])? $_POST['channel'] : $_GET['channel'];

BUT on my server I keep getting this response

<br /> <b>Notice</b>: Undefined index: data in <b>C://xampp/htdocs/server/index.php</b> on line <b>30</b><br /> <br /> <b>Notice</b>: Undefined index: token in <b>C://xampp/htdocs/server/index.php</b> on line <b>31</b><br /> <br /> <b>Notice</b>: Undefined index: channel in <b>C://xampp/htdocs/server/index.php</b> on line <b>32</b><br />

Pls Im new to bash scripting, need your help. Thanks.

  • based on your PHP implementation, you expect 3 separate fields, and you are attempting to send just one json. – Sorin Mar 11 '20 at 13:06

1 Answers1

-1

json supports double quotes

curl -H "Content-type: application/json" \
 -H "Accept: application/json" -sb \
-d "{\"token\":\"token-key-here\",\"channel\":\"hybridteam-ngrok\",\"appname\":\"This is testing PC\", \"data\": ${tunnels}}" \
-X POST  "https://localhost:8080"

and you need to read raw POST body and convert it either into object or to array.

$raw = file_get_contents("php://input");
$data = json_decode($raw,true);
$token = isset($data['token'])? $data['token'] : null;
$channel = isset($data['channel'])? $data['channel'] : null;
Maxim Sagaydachny
  • 2,098
  • 3
  • 11
  • 22