【问题标题】:Perl HTTP::Request with File Upload ignores Headers带有文件上传的 Perl HTTP::Request 忽略标头
【发布时间】:2015-07-10 21:22:25
【问题描述】:

我正在尝试通过 HTTP::Request 在 Perl 中使用 Content-Type 的“form-data”上传文件。

代码如下:

$request = POST $URL,
                $headers,
                Content_Type => 'form-data',
                Content => [
                  'filename' => $args->{ formData }->{ filename },
                  'options'  => $args->{ formData }->{ options }
                ];

$headers 是使用以下方法构建的:

my $headers = HTTP::Headers->new();
while ( my ( $name, $value ) = each %{ $args->{ headers } } ) {
    $headers->header( $name, $value );
}

当调用其他不通过此调用进行文件上传的事情时,这非常有效:

$request = HTTP::Request->new( 'POST', $uri, $headers );

当我查看文件上传调用的输出时,它没有任何标题信息,除了:

Content-Length: 918
Content-Type: multipart/form-data; boundary=xYzZY

但是,应该在其中设置另一个值。

如果我删除“$headers”,输出是相同的。我也尝试过使用

HEADERS => $headers,

但这只会产生一个带有 HEADERS 键和 HASH(0x........) 值的标题

我也试过添加

X-key-name => $value,

但这给出了一个错误。用引号将键括起来不会将其添加到标题中。

在过去的几个小时里,我一直在徒劳地寻找一个包含文件上传和标题的 POST 示例。

我的想法已经不多了。

【问题讨论】:

    标签: perl http-headers httprequest


    【解决方案1】:

    请注意

     HTTP::Request::Common::POST(...)
    

    不一样
    HTTP::Request->new(POST => ...)
    

    但论据不同。

    根据HTTP::Request::Common中的文档

       POST $url
       POST $url, Header => Value,...
       POST $url, $form_ref, Header => Value,...
       POST $url, Header => Value,..., Content => $form_ref
       POST $url, Header => Value,..., Content => $content
    

    没有使用 HTTP::Headers 对象作为第二个参数的记录方法。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-06-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-02-08
      • 1970-01-01
      • 2018-09-04
      • 1970-01-01
      相关资源
      最近更新 更多