http://qs321.pair.com?node_id=1122236

xpert has asked for the wisdom of the Perl Monks concerning the following question:

I want to send 2 different files to an API. File-1 contains XML data and File-2 is a MS word document. I am using following code:

my $signedURL = ‘signedURL'; my $file = ‘C:\some.docx'; my $text = read_file( ‘C:\some.xml’ ) ; my $ua = LWP::UserAgent->new; my $req = $ua->request(PUT $signedURL, Content_Type => ‘form-data’, Content => [ XML => $text, Upload => [“$file”] ] ); print “\nRESPONSE — \n” . $req->as_string; # Check the outcome of the response if ($req->is_success) { print $req->content; } else { print “\n in else not success\n”; }

It is generating following error:

Not a SCALAR reference at C:/Dwimperl/perl/vendor/lib/HTTP/Message.pm +line 156. at C:/Dwimperl/perl/vendor/lib/HTTP/Message.pm line 156 HTTP::Message::add_content(‘HTTP::Request=HASH(0x34f4f54)’, ‘ARRAY(0x3 +4e 84bc)’) called at C:/Dwimperl/perl/vendor/lib/HTTP/Request/Common.pm l +ine 108 HTTP::Request::Common::_simple_req(undef, undef, undef, undef) called +at C:/Dwimperl/perl/vendor/lib/HTTP/Request/Common.pm line 22 HTTP::Request::Common::PUT(‘http://api.aspose.com/v1.1/words/executeMa +il Merge?withRegions…’, ‘Content_Type’, ‘form-data’, ‘Content’, ‘ARRAY(0x +34e84bc) ‘) called at word_multipart2.pl line 50 Press any key to continue . . .

Please, guide

Replies are listed 'Best First'.
Re: PUT a Multipart request in PERL
by Anonymous Monk on Apr 02, 2015 at 08:44 UTC

      Hi Monk, Here is my code:

      use WWW::Mechanize; my $mech = WWW::Mechanize->new(); my $xmlfile = [ 'C:\\temp\\Test_1.xml', 'Test_1.xml', 'Content-type' => 'text/xml' ]; my $docfile = [ 'C:\\temp\\Test_1.doc', 'Test_1.doc', 'Content-type' => 'application/msword' ]; my $signedurl = 'http://someURL.com'; my $myresponse = $mech->put($signedurl, 'Content' => [ 'file1' => +$xmlfile, 'file2' => $docfile ] );

      It is producing following ERROR:

      Not a SCALAR reference at C:/Dwimperl/perl/vendor/lib/HTTP/Message.pm +line 156. at C:/Dwimperl/perl/vendor/lib/HTTP/Message.pm line 156 HTTP::Message::add_content('HTTP::Request=HASH(0x32e2d9c)', 'ARRAY(0x3 +2113f4)') called at C:/Dwimperl/perl/vendor/lib/HTTP/Request/Common.p +m line 108 HTTP::Request::Common::_simple_req(undef, undef) called at C:/Dwimperl +/perl/vendor/lib/HTTP/Request/Common.pm line 22 HTTP::Request::Common::PUT('http://someURL.com.../', 'Content', 'ARRAY +(0x32113f4)') called at C:/Dwimperl/perl/site/lib/WWW/Mechanize.pm line 439 WWW::Mechanize::_SUPER_put('WWW::Mechanize=HASH(0x30a72bc)', 'http://someURL.com/...', 'Content', 'ARRAY(0x32113f4)') called at C:/Dwimperl/perl/site/lib/WWW/Mechanize.pm line 430 WWW::Mechanize::put('WWW::Mechanize=HASH(0x30a72bc)', 'http://someURL. +com/...', 'Content', 'ARRAY(0x32113f4)') called at multipart_1.pm line 110 Utils::Words_multipart('Utils=HASH(0x30a49b4)') called at multipart_1. +pm line 138 Press any key to continue . . .

      Please guide

        Please guide

        I already did, which part was unclear?

Re: PUT a Multipart request in PERL ( lwp-mechanize-put-multipart-form.pl )
by Anonymous Monk on Apr 13, 2015 at 09:08 UTC

    Doing what I said seems to work to me

    #!/usr/bin/perl -- use strict; use warnings; use WWW::Mechanize 1.73; my $ua = WWW::Mechanize->new; $ua->timeout(0.1); $ua->add_handler("request_send", sub { shift->dump; return }); $ua->add_handler("response_done", sub { shift->dump; return }); $ua->show_progress(1); $ua->put_multi( 'http://localhost', Content_Type => 'multipart/form-data', Content => [ fileuploadfieldname => [ undef, # filename_to_read or none in this case "filenamesent", -content => 'i sent you some content named filenamesent' ], ], ); sub WWW::Mechanize::put_multi { my( $self, $uri, @parameters ) = @_; $uri = $uri->url if ref($uri) eq 'WWW::Mechanize::Link'; $uri = $self->base ? URI->new_abs( $uri, $self->base ) : URI->new( $uri ); unshift @parameters, $uri->as_string; require HTTP::Request::Common; my @suff = $self->_process_colonic_headers(\@parameters,1); my $req = HTTP::Request::Common::POST( @parameters, ); $req->method('PUT'); return $self->request( $req, @suff ); } __END__ $ perl lwp-mechanize-put-multipart-form.pl ** PUT http://localhost ==> PUT http://localhost Accept-Encoding: gzip User-Agent: WWW-Mechanize/1.73 Content-Length: 163 Content-Type: multipart/form-data; boundary=xYzZY --xYzZY\r Content-Disposition: form-data; name="fileuploadfieldname"; filename=" +filenamesent"\r -Content: i sent you some content named filenamesent\r \r \r --xYzZY--\r\n 500 Can't connect to localhost:80 Content-Type: text/plain Client-Date: Thu, 02 Apr 2015 08:49:31 GMT Client-Warning: Internal response Can't connect to localhost:80\n No connection could be made because the target machine actively refuse +d it. at .../site/lib/LWP/Protocol/http.pm line 49.\n 500 Can't connect to localhost:80 (1s) Error PUTing http://localhost: Can't connect to localhost:80 at lwp-me +chanize-put-multipart-form.pl line 62.