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

Hi fellow Monks

I'm trying to put a file on the web server, the file is in the same folder as the script, but I'm doing something wrong

Can you please point out where I'm going wrong

Kind Regards

#!c:\perl\bin -w use LWP::UserAgent; use HTTP::Request::Common; my $url = "http://192.168.0.200/maintenance/instructions"; my $fname = "test.txt"; my $ua = LWP::UserAgent->new(); my $req = POST $url, Content_Type => 'form-data', Content => [ submit => 1, upfile => [ $fname ] ]; my $response = $ua->request($req); if ($response->is_success()) { print "OK: ", $response->content; } else { print "Failed: ", $response->as_string; } exit;

Replies are listed 'Best First'.
Re: Put file on webserver via HTTP
by EvanCarroll (Chaplain) on Nov 01, 2005 at 07:25 UTC

    I don't think your posting the file, I think your posting the content of $fname as postvariable 'upfile'.

    Look at the example in 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'. If one of the values in the $form_ref is an array reference, then it is treated as a file part specification with the following values:
    [ $file, $filename, Header => Value... ]

    Yet your array refrence points to an array with only one value, the $filename.


    Evan Carroll
    www.EvanCarroll.com
      You need to look further down in the documentation, where it says
      The $filename is the filename to report in the request. If this value is undefined, then the basename of the $file will be used.
      so no, I don't think that's what the problem is. I would start by making sure that "/maintenance/instructions" is in fact a script that expects a file to be posted from a form, and also take a look at what output that script is producing when you call it in this way.
Re: Put file on webserver via HTTP
by pajout (Curate) on Nov 01, 2005 at 07:21 UTC
    I don't know, what following does mean,
    my $req = POST $url,
    ,I typically write
    $req = new HTTP::Request(POST => $url);
        I am sorry, my mistake...
    A reply falls below the community's threshold of quality. You may see it by logging in.
Re: Put file on webserver via HTTP
by Moron (Curate) on Nov 01, 2005 at 15:50 UTC
    Although the webserver knows where to find your script, it doesn't set the default directory to there when executing it. I use apache on linux, so the actual default directory for the server will be different from yours, but in any case you will need to study the webserver documentation to find out how to map and alias between URLs and the file system - it isn't as simple a matter as there being a single place for this file.

    -M

    Free your mind

Re: Put file on webserver via HTTP
by monarch (Priest) on Nov 02, 2005 at 01:11 UTC
    Funnily enough I'm about to attempt the exact same thing in a moment.. No doubt there will be a corresponding update to this post.

    Two things: WWW::Mechanize is rumoured to be able to handle file uploads. Secondly, when posting a file the HTML form source must specify enctype:

    <form ... enctype="multipart/form-data">
    ..so perhaps that needs to be fed into LWP::UserAgent if you are going to post using that technique?

    Update: I tried WWW::Mechanize but crashed and burned because I couldn't work out how to get a proxy going with SSL on a Win32 machine. So instead I wrote the following subroutine:

    Tested on Win32 with XP uploading to a HTTPS URL. Oh, and note that this technique is documented by the HTTP::Request::Common module.