[英]Perl — HTTP::Request::Common — POST file AND array
I'm trying to POST file into a webapp endpoint. 我正在尝试将文件POST到webapp端点。 The problem is that in the same request I need to include array as a value of one of the parameters.
问题是在同一个请求中我需要将数组包含为其中一个参数的值。
my $ua = LWP::UserAgent->new();
my $response = $ua->post(
$baseurl_local . 'create',
Content_Type => 'form-data',
Content => [
file => [$file],
targetLang => 'french',
]);
works just fine. 工作得很好。
However when I try to 但是,当我尝试
my $ua = LWP::UserAgent->new();
my $response = $ua->post(
$baseurl_local . 'create',
Content_Type => 'form-data',
Content => [
file => [$file],
targetLang => ['french','spanish],
]);
I get 我明白了
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
无法打开文件法语:C:/Strawberry/perl/site/lib/HTTP/Request/Common.pm第154行没有这样的文件或目录.HTTP :: Request :: Common :: form_data(ARRAY(0x6800698), undef,HTTP :: Request = HASH(0x6803a70))在C:/Strawberry/perl/site/lib/HTTP/Request/Common.pm第67行调用
It would seem Perl thinks array ref with languages is a file. 看起来Perl认为使用语言的数组引用是一个文件。
what am I doing wrong? 我究竟做错了什么?
To expand on the resolution based on Matt's answer: as I forgot to mention originally, the list of languages comes from user input, so I ended up doing something like: 根据Matt的答案扩展分辨率:我最初忘了提及,语言列表来自用户输入,所以我最终做了类似的事情:
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,
]);
From the HTTP::Request::Common docs : 从HTTP :: Request :: Common文档 :
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.POST方法还支持RFC 1867中指定的用于基于表单的文件上载的
multipart/form-data
内容。您可以通过将'form-data'
的内容类型指定为请求标头之一来触发此内容格式。 If one of the values in the$form_ref
is an array reference, then it is treated as a file part specification...如果
$form_ref
中的某个值是数组引用,则将其视为文件部件规范...
So, while you could normally specify a multivalued field with an array reference, array references have special significance with multipart/form-data
content, and you'll probably have to get around it by repeating the field name: 因此,虽然您通常可以使用数组引用指定多值字段,但数组引用对于
multipart/form-data
内容具有特殊意义,您可能必须通过重复字段名称来解决它:
my $response = $ua->post(
$baseurl_local . 'create',
Content_Type => 'form-data',
Content => [
file => [$file],
targetLang => 'french',
targetLang => 'spanish',
],
);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.