gpt4 book ai didi

perl - www::curl - 如何上传(发布)大文件

转载 作者:行者123 更新时间:2023-12-04 18:12:58 24 4
gpt4 key购买 nike

我使用 WWW::Curl 上传文件:

use WWW::Curl::Easy 4.14;
use WWW::Curl::Form;

my $url = 'http://example.com/backups/?sid=12313qwed323';
my $params = {
name => 'upload',
action => 'keep',
backup1 => [ '/tmp/backup1.zip' ], # 1st file for upload
};

my $form = WWW::Curl::Form->new();
foreach my $k (keys %{$params}) {
if (ref $params->{$k}) {
$form->formaddfile(@{$params->{$k}}[0], $k, 'multipart/form-data');
} else {
$form->formadd($k, $params->{$k});
}
}

my $curl = WWW::Curl::Easy->new() or die $!;
$curl->setopt(CURLOPT_HTTPPOST, $form);
$curl->setopt(CURLOPT_URL, $url);

my $body;
$curl->setopt(CURLOPT_WRITEDATA, \$body);
my $retcode = $curl->perform();
my $response_code = $curl->getinfo(CURLINFO_HTTP_CODE);

这里没什么特别的,这段代码运行良好。

我想上传大文件,我不想预加载内存中的所有内容。至少那是我听说 libcurl 正在做的事情。

CURLOPT_READFUNCTION 接受返回部分内容的回调。这意味着我不能使用 WWW::Curl::Form 来设置 POST 参数,但我必须通过这个回调返回整个内容。是对的吗?

我认为代码可能如下所示:
use WWW::Curl::Easy 4.14;

my $url = 'http://example.com/backups/?sid=12313qwed323'
my $params = {
name => 'upload',
action => 'keep',
backup1 => [ '/tmp/backup1.zip' ], # 1st file for upload
};

my $fields;
foreach my $k (keys %{$params}) {
$fields .= "$k=".(ref $params->{$k} ? '@'.@{$params->{$k}}[0] : uri_escape_utf8($params->{$k}))."&";
}
chop($fields);

my $curl = WWW::Curl::Easy->new() or die $!;
$curl->setopt(CURLOPT_POST, 1);
$curl->setopt(CURLOPT_POSTFIELDS, $fields); # is it needed with READFUNCTION??
$curl->setopt(CURLOPT_URL, $url);

my @header = ('Content-type: multipart/form-data', 'Transfer-Encoding: chunked');
$curl->setopt(CURLOPT_HTTPHEADER, \@header);

#$curl->setopt(CURLOPT_INFILESIZE, $size);
$curl->setopt(CURLOPT_READFUNCTION, sub {

# which data to return here?
# $params (without file) + file content?

return 0;
});

CURLOPT_READFUNCTION 回调必须返回哪些数据? $params + 文件内容?采用哪种格式?

我真的必须自己创建数据(由 CURLOPT_READFUNCTION 返回)还是有一种简单的方法可以以正确的格式创建数据?

谢谢

最佳答案

测试 16formpost.t是相关的。如您所见,它已完全禁用。这个事实和我对回调函数的各种返回值的徒劳实验让我相信 CURLOPT_READFUNCTION已知功能在 Perl 绑定(bind)中被破坏。

I have to return the whole content through this callback. Is that right?


不,您可以分段提供请求正文,适用于分 block 编码。根据 CURLOPT_INFILESIZE 中设置的限制,回调必然会被调用多次。 .

Which data does CURLOPT_READFUNCTION callback have to return?


HTTP 请求正文。由于您进行文件上传,这意味着 Content-Type multipart/form-data .以下是使用 HTTP::Message 的示例。 CURLOPT_HTTPPOST是另一种构造这种格式的方法。
use HTTP::Request::Common qw(POST);
use WWW::Curl::Easy 4.14;

my $curl = WWW::Curl::Easy->new or die $!;
$curl->setopt(CURLOPT_POST, 1);
$curl->setopt(CURLOPT_URL, 'http://localhost:5000');
$curl->setopt(CURLOPT_HTTPHEADER, [
'Content-type: multipart/form-data', 'Transfer-Encoding: chunked'
]);
$curl->setopt(CURLOPT_READFUNCTION, sub {
return POST(undef, Content_Type => 'multipart/form-data', Content => [
name => 'upload',
action => 'keep',
backup1 => [ '/tmp/backup1.zip' ], # 1st file for upload
])->content;
});
my $r = $curl->perform;

关于perl - www::curl - 如何上传(发布)大文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9485157/

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