gpt4 book ai didi

perl - 使用 curl 发布 Gzip 压缩数据

转载 作者:行者123 更新时间:2023-12-04 19:40:00 25 4
gpt4 key购买 nike

我试图使用系统 curl 将 gzipped 数据发布到服务器,但我一直以奇怪的错误告终

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


curl: no URL specified!


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


sh: -c: line 0: unexpected EOF while looking for matching `"'
sh: -c: line 1: syntax error: unexpected end of file

最佳答案

添加 "是朝着正确方向迈出的一步,但您没有考虑到 $data可能包含 " , $等。您可以使用 String::ShellQuote来解决这个问题。

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`;

或者您可以完全避免使用 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);

或者,如果您实际上并不需要捕获输出,以下内容也可以完全避免使用 shell:
system(
curl => (
'-sS',
'-X' => 'POST',
'-H' => 'Content-Type: application/gzip',
'--data-binary' => $data,
$url,
),
);

也就是说,我看不出你怎么可能发送包含 NUL 字节的字符串,这是一个 gzipped 文件可能有的东西。我认为你的方法本质上是有缺陷的。

你知道吗 libcurl ( curl 的内容)可以通过 Net::Curl::Easy 访问?

关于perl - 使用 curl 发布 Gzip 压缩数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20528053/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com