gpt4 book ai didi

Perl — HTTP::Request::Common — POST 文件和数组

转载 作者:行者123 更新时间:2023-12-02 20:42:50 24 4
gpt4 key购买 nike

我正在尝试将 POST  文件发送到 Web 应用程序端点。问题是,在同一个请求中,我需要包含数组作为参数之一的值。

my $ua = LWP::UserAgent->new();
my $response = $ua->post(
$baseurl_local . 'create',
Content_Type => 'form-data',
Content => [
file => [$file],
targetLang => 'french',
]);

工作得很好。

但是当我尝试

my $ua = LWP::UserAgent->new();
my $response = $ua->post(
$baseurl_local . 'create',
Content_Type => 'form-data',
Content => [
file => [$file],
targetLang => ['french','spanish],
]);

我明白了

Can't open file french: No such file or directory at C:/Strawberry/perl/site/lib/HTTP/Request/Common.pm line 154. HTTP::Request::Common::form_data(ARRAY(0x6800698), undef, HTTP::Request=HASH(0x6803a70)) called at C:/Strawberry/perl/site/lib/HTTP/Request/Common.pm line 67

Perl 似乎认为带有语言的数组引用是一个文件。

我做错了什么?

<小时/>

根据马特的回答扩展解决方案:正如我最初忘记提及的那样,语言列表来自用户输入,所以我最终做了类似的事情:

my @languages = (targetLang => 'french', targetLang => 'spanish');
my $ua = LWP::UserAgent->new();
my $response = $ua->post(
$baseurl_local . 'create',
Content_Type => 'form-data',
Content => [
file => [$file],
@languages,
]);

最佳答案

来自HTTP::Request::Common docs :

Multivalued form fields can be specified by either repeating the field name or by passing the value as an array reference.

The POST method also supports the multipart/form-data content used for Form-based File Upload as specified in RFC 1867. You trigger this content format by specifying a content type of 'form-data' as one of the request headers. If one of the values in the $form_ref is an array reference, then it is treated as a file part specification...

因此,虽然您通常可以使用数组引用指定多值字段,但数组引用对于 multipart/form-data 内容具有特殊意义,您可能必须通过重复来绕过它字段名称:

my $response = $ua->post(
$baseurl_local . 'create',
Content_Type => 'form-data',
Content => [
file => [$file],
targetLang => 'french',
targetLang => 'spanish',
],
);

关于Perl — HTTP::Request::Common — POST 文件和数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50705344/

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