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

I am trying to use LWP to post a multi-part form to upload a file from my local machine.
I keep getting the following response:

cgi-lib.pl: reached end of input while seeking boundary of multipart. Format of CGI input is wrong.

What am I doing wrong? My code is as follows:
#!/usr/bin/perl # Create a user agent object use LWP::UserAgent; $ua = new LWP::UserAgent; $ua->agent("AgentName/0.1 " . $ua->agent); $URL="http://www.mydomain.com/cgi-bin/upload.pl"; #uses cgi-lib218.pl + # Create a request my $req = new HTTP::Request POST => $URL; $req->content_type('multipart/form-data; boundary=lwp234892.3',['file' +,'C:\WINDOWS\Desktop\test.txt']); $req->content('table=mytest&name=lwptest1'); # Pass request to the user agent and get a response back my $res = $ua->request($req); # Check the outcome of the response if ($res->is_success) { print $res->content; } else { print "Bad luck this time\n"; }

Edit kudra, 2002-04-23 s/br/code/

  • Comment on cgi-lib error when using lwp multipart form to upload a file - what am I doing wrong?
  • Download Code

Replies are listed 'Best First'.
Re: cgi-lib error when using lwp multipart form to upload a file - what am I doing wrong?
by grep (Monsignor) on Apr 23, 2002 at 06:10 UTC

    cgi-lib.pl has been retired for several years (horribly broken).

    You best bet is to port your CGI script over to the current CGI Module CGI.pm. Not only has file uploading been improved, but all current development is focused towards CGI.pm, so you can gain the benefits of other current modules like CGI::Carp and CGI::Safe.



    grep
    Unix - where you can thrown the manual on the keyboard and get a command
      I have modified the cgi-lib quite a bit to meet other needs so I would rather not go away from it. If I post a multi-part form manually to my upload.pl script, it works beautifully so I do not think that is the problem. Isn't there something wrong with the way I am using LWP? Please help.

        What have you done with cgi-lib? If what you have done is specific to you, then why not use both? CGI.pm is better at handling this sort of situation, and the object oriented interface won't interfere with imported cgi-lib functions. At the *very least* use CGI to test and make sure it isn't a cgi-lib problem, or even a bug in you adaptations (both of which are likely).

        If your cgi-lib mods are useful to others, consider subclassing CGI.pm for these modifications. This is the only way to get your improvements passed on to anyone else, as cgi-lib maintainence has ceased.

        Cheers,
        Erik