Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!


Summoning BASH masters!
New on LowEndTalk? Please Register and read our Community Rules.

All new Registrations are manually reviewed and approved, so a short delay after registration may occur before your account becomes active.

Summoning BASH masters!

LeviLevi Member

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.

    Thanked by 2uptime Levi
  • uptimeuptime Member
    edited August 2019

    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

     
    $ json='
    {
    "website_url": "${site_url}",
    "web_dir": "${www_dir}",
    "php_fpm_version": "7.3",
    "server_id": "1"
    }'
    
    $ echo $json
    { "website_url": "${site_url}", "web_dir": "${www_dir}", "php_fpm_version": "7.3", "server_id": "1" }
    
    $ echo "${json}"
    
    {
    "website_url": "${site_url}",
    "web_dir": "${www_dir}",
    "php_fpm_version": "7.3",
    "server_id": "1"
    }
    

    maybe try this instead:

    $ json='{
    "website_url": "${site_url}",
    "web_dir": "${www_dir}",
    "php_fpm_version": "7.3",
    "server_id": "1"
    }'
    
    Thanked by 1Levi
  • On the PHP side, try:

    <?php
    
    $data = file_get_contents('php://input');
    $jsonData = json_decode($data);
    
    // $jsonData should contain the body
    var_dump($jsonData);
    

    Haven't tested this, but I remember dealing with this before and POST wasn't returning anything.

    Thanked by 1Levi
  • 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.

    Thanked by 2Falzo uptime
  • LeviLevi Member

    Thank you guys! Problem fixed.

    BASH:

    json='{
    "website_url": "${site_url}",
    "web_dir": "${www_dir}",
    "php_fpm_version": "7.3",
    "server_id": "1"
    }'

    I removed any spaces at the beginning of each line.

    PHP advice taken from @definitelyliam

  • LTniger said: I removed any spaces at the beginning of each line.

    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"

Sign In or Register to comment.