0
votes

I am trying to upload a file using Content-Type of 'form-data' in Perl with HTTP::Request.

The code looks like this:

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

$headers is built using:

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

This is working PERFECTLY when calling other things that do not do file uploads with this call:

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

When I look at the output from the file upload call, it does not have any header information other than:

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

However, there is another value set which should be in there.

If I remove "$headers" the output is identical. I have also tried using

HEADERS => $headers,

but this just produces a header with the key of HEADERS, and a value of HASH(0x........)

I also tried adding

X-key-name => $value,

but that gives an error. Surrounding the key with quotes doesn't add it to the headers.

I have looked for the past several hours in vain for even ONE example where a POST was done containing a file upload AND headers.

I'm running out of ideas.

1

1 Answers

2
votes

Please note that

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

is not the same as

HTTP::Request->new(POST => ...)

but that the arguments differ.

According to the documentation in 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

there is no documented way to use a HTTP::Headers object as the second argument.