Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I POST a multipart HTTP request from Perl to Java and get a response?

I'm trying to post from one of my subroutines in Perl a request to a Java based controller. But I'm not getting any kind of response back. I know the Java code works file because I can get a response if I post to it from a HTML form.

This is my Perl code:

  use HTTP::Request::Common;
  my $ua = LWP::UserAgent->new;

  my $response = $ua->request(POST 'http://testserver/testing.nc',
        Content_Type => 'form-data',
        Content => [
            method => 'submit',
            ftp_server => 'ftp.localhost',
            ftp_user => 'testuser',
            ftp_password => 'testpass',
            remote_path => '/home/files',
            port => 22,
            file_to_upload => ["$file"]
  ]);

Is there something wrong with this code?

like image 205
goe Avatar asked Feb 25 '10 13:02

goe


1 Answers

Posted data must be of type multipart/form-data.

Edit: OK, so it turns out, specifying form-data is enough as mentioned in the HTTP::Request::Common docs:

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.

However, to use HTTP::Request::Common::POST the way you are using, you will need to import POST:

use HTTP::Request::Common qw(POST);

or use $ua->post:

The post(...) method of LWP::UserAgent exists as a shortcut for $ua->request(POST ...).

You can make your life easier by using WWW::Mechanize. See also this upload example.

like image 150
Sinan Ünür Avatar answered Oct 09 '22 00:10

Sinan Ünür