Publish Gzip Data with Curl

im trying to use the system curl to send gzipped data to the server, but I continue to end up with strange errors

`curl -sS -X POST -H "Content-Type: application/gzip" --data-binary $data $url`

gives

curl: no URL specified!

and

`curl -sS -X POST -H "Content-Type: application/gzip" --data-binary "$data" $url`

gives

sh: -c: line 0: unexpected EOF while looking for matching `"'
sh: -c: line 1: syntax error: unexpected end of file
+4
source share
3 answers

Adding "is a step in the right direction, but you don’t think it $datamight contain ", $etc. You can use String :: ShellQuote to solve this problem.

use String::ShellQuote qw( shell_quote );

my $cmd = shell_quote(
   curl => (
      '-sS',
      '-X' => 'POST',
      '-H' => 'Content-Type: application/gzip',
      '--data-binary' => $data,
      $url,
   ),
);

my $output = `$cmd`;

Or you can completely abandon the shell.

my @cmd = (
   curl => (
      '-sS',
      '-X' => 'POST',
      '-H' => 'Content-Type: application/gzip',
      '--data-binary' => $data,
      $url,
   ),
);

open(my $pipe, '-|', @cmd) or die $!;
my $output = do { local $/; <$pipe> };
close($pipe);

Or, if you really don't need to write output, the following also completely excludes the shell:

system(
   curl => (
      '-sS',
      '-X' => 'POST',
      '-H' => 'Content-Type: application/gzip',
      '--data-binary' => $data,
      $url,
   ),
);

, , , NUL-, gzipped. , .

, libcurl ( curl) Net:: Curl:: Easy?

+4

, stdin, , :

curl -sS -X POST -H "Content-Type: application/gzip" --data-binary @<(echo "Uncompressed data" | gzip) $url

.

+3

, , , . - .

0

All Articles