Beefy Boxes and Bandwidth Generously Provided by pair Networks Joe
Problems? Is your data what you think it is?
 
PerlMonks  

Put file on webserver via HTTP

by PugSA (Beadle)
on Nov 01, 2005 at 01:44 UTC ( [id://504526]=perlquestion: print w/replies, xml ) Need Help??

This is an archived low-energy page for bots and other anonmyous visitors. Please sign up if you are a human and want to interact.

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 pajout (Curate) on Nov 01, 2005 at 02:21 UTC
    I don't know, what following does mean,
    my $req = POST $url,
    ,I typically write
    $req = new HTTP::Request(POST => $url);
      good point, more likely the problem.


      Evan Carroll
      www.EvanCarroll.com

        My apologies for my lack of knowledge, but how do I make the request,

        $req = new HTTP::Request(POST => $url);

        post a file. And where do I specify this file

        I am sorry, my mistake...
Re: Put file on webserver via HTTP
by EvanCarroll (Chaplain) on Nov 01, 2005 at 02: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 Moron (Curate) on Nov 01, 2005 at 10: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 01, 2005 at 20: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.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://504526]
Approved by Corion
help
Sections?
Information?
Find Nodes?
Leftovers?
    Notices?
    hippoepoptai's answer Re: how do I set a cookie and redirect was blessed by hippo!
    erzuuliAnonymous Monks are no longer allowed to use Super Search, due to an excessive use of this resource by robots.