H-street has asked for the wisdom of the Perl Monks concerning the following question:

Hoping i can get some help.. Trying to get the http::request working for multipart form data I am attempting to submit files through a multipart/form-data here is the code
my $reg = HTTP::Request->new("POST"=>"https://$server/upload"); $reg->content_type("multipart/form-data"); $reg->header("X-Update-Nonce"=>$nonce); $reg->content([ 'label'=>'TestFile.pdf', 'file'=>['test.pdf'] ]); print "REQUEST\n".$reg->as_string();

A couple of problems, i have to use the header multipart/form-data or else the server returns an error. Where the documentation for http::request::common says to use content-type of form-data alone? (content_type('form-data') vs ('multipart/form-data'))

If i just use "form-data" i get a 400 Bad Request, the header has to be multipart/form-data

When i print out the Request it isn't converting the "Content" correctly, it is printing out the Array Ref instead. I've tried to format the content different ways but none of them have produced what i need. In the documentation of HTTP::Request it specifies that content is a string of bytes.. I'm not sure what they are referring too, i haven't been able to get any of the example code in the documentation to work like it should.

This is what i prints out

POST https://xxxxx/upload Content-Type: multipart/form-data X-Update-Nonce: XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX ARRAY(0x9dec0d8)

The array is where the content is suppose to be but i haven't gotten it to fill out correctly yet.

Any Ideas? - Thanks

Replies are listed 'Best First'.
Re: multipart/form-data content problem
by tobyink (Canon) on Mar 14, 2012 at 15:54 UTC

    You are trying to use an arrayref as the content for an HTTP POST. The content should be a single, properly formatted string. You are presumably expecting HTTP::Request to convert that arrayref into a string. It will not. It does not have that feature.

    HTTP::Request::Common does have that feature though.

    perl -E'sub Monkey::do{say$_,for@_,do{($monkey=[caller(0)]->[3])=~s{::}{ }and$monkey}}"Monkey say"->Monkey::do'
Re: multipart/form-data content problem
by Anonymous Monk on Mar 14, 2012 at 15:14 UTC
      That was it thanks! although i had to change one thing, on the parse($req) i had to make it parse($req->as_string().