All new Registrations are manually reviewed and approved, so a short delay after registration may occur before your account becomes active.
Summoning BASH masters!
I'am writing some automation script in bash and in final step I want that script send some data to remote server via CURL. Data is in JSON format, data receiver is written in PHP.
I have done numerous tests, but no luck to receive answer from PHP script after CURL request is made.
I've done this:
BASH side (sender):
!/bin/bash
json='
{
"website_url": "${site_url}",
"web_dir": "${www_dir}",
"php_fpm_version": "7.3",
"server_id": "1"
}'echo "${json}" | curl --request POST "https://website.tld/api/api.php" \
-H "Content-Type: application/json" \
-d @-
PHP side (receiver):
<?php
print_r($_POST);
Expected results:
Array(
{
"website_url": "${site_url}",
"web_dir": "${www_dir}",
"php_fpm_version": "7.3",
"server_id": "1"
}
)
Actual results:
Aray
(
)
Comments
I'd try eliminating the newline at the start of the json string, and if that doesn't work use wireshark to make sure the json is actually getting sent, and if that doesn't work start debugging on the server side.
Imma maybe go out on a limb to say this looks more like a "curl / PHP" question than something to do specifically with teh bash ...
oh wait ... yeah - here's a clue
maybe try this instead:
On the PHP side, try:
Haven't tested this, but I remember dealing with this before and POST wasn't returning anything.
A few thoughts on quickly debugging things:
a) Try to save the data to a file (mktemp is your friend) and use curl to submit post data from the file as contents. This will eliminate any quoting suspicions.
b) If (a) works, it will confirm that there's some quoting diff - you can then try a
cat file | curl ...
to see if that helps instead of the echo. Maybe using bash arrays which handle quotes much better will help or else constructing a string via$(cat ...)
etc. may help.Quoting has been a pain in many cases in my experiences as well.
Thank you guys! Problem fixed.
BASH:
I removed any spaces at the beginning of each line.
PHP advice taken from @definitelyliam
It's probably the whitespace before the opening
{
that was the culprit (ideally and in theory, white space in between shouldn't cause an issue).spaces="the bane of bash"