perlbashcurlhttp-post

How to pass a string with spaces as http POST data using curl


I am running a perl script that calls a bash script with arguments. It reads a string from a socket, splits it and saves it to an array. This is how such a string looks:

  22.12.13 20:21:12;RING;0

The first array element is a date, which is passed as an argument to a bash script. In the bash script, the arguments are passed on to curl as POST data.

I don't know how to handle the space between date and time in the string. The curl is executed correctly and the server responds with OK (200). However it's only the first part (up to the space) that is passed as POST data. The time is missing. curl gives the folowing error:

curl: (6) Couldn't resolve host '20:21'

These are the relevant lines from the scripts:

     #perl script
     my $EXTPRO="/path/to/script.sh"; 
if ($_ =~ /RING/){ 
     my @C = split(/;/); 
     my @args = ($EXTPRO, $nr, $C[0], "String"); 
     system(@args);
#bash script:
curl http://url.com/add -F foo=$3 -F date=$2 -F foobar=$1

Solution

  • The time is being passed correctly, it's just your bash script that needs to be edited as your error reflects.

    curl http://url.com/add -F foo=String -F date=22.12.13 20:21:12 -F foobar=Fake_NR_Data
    

    The time is being treated as a second URL. To get your bash to work correctly, just enclose $2 in quotes

    #bash script:
    curl http://url.com/add -F foo=$3 -F date="$2" -F foobar=$1