Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical
 
PerlMonks  

How do I upload a file to a https server.

by Anonymous Monk
on Jun 01, 2004 at 16:20 UTC ( [id://358180]=perlquestion: print w/replies, xml ) Need Help??

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

Hi - I need to get and put files onto a https server. The get is working OK with something like:
my $browser = LWP::UserAgent->new; $browser->credentials( $ENV{'HTTPS_SITE'}, $ENV{'HTTPS_REALM'}, $ENV{'HTTPS_USERNAME'} => $ENV{'HTTPS_PASSWORD'} ); my $response = $browser->get($url_filename);
However, I don't understand how to put a file there. I have tried POST, but not really sure how this works as I have read a lot of articles talking about forms. Is this the correct command to use to upload a file? Thanks Jonathan

Replies are listed 'Best First'.
Re: How do I upload a file to a https server.
by iburrell (Chaplain) on Jun 01, 2004 at 16:34 UTC
    File uploads should be the same as with HTTP. You might want to get it working with HTTP if you can and then switch to HTTPS. Look at the HTTP::Request::Common, POST method.
    my $request = POST($url, [ 'param' => 'value', 'file_param' => [ $filename ] ]); my $reponse = $browser->request($request);
    Replace file_param with the name of the "file" type input param.
      Thanks for your help. I have tried the following which uses 'post' directly and not 'request' as in your code:
      my $browser = LWP::UserAgent->new; ... my $response = $browser->post($url, [ 'file' =>[ "/tmp/test_file.txt" +] ]);
      I get "405 Method not allowed". Does something need configuring on the server or is the code wrong? (I have tried http and https servers). Thanks Jonathan
        That error code is coming from the server. It means that the server does not allow POSTs to that URL. Most likely, the URL you are posting to is not a script. POST uploads need a script that handle processing the form submission and saves the uploaded file.

        PUT is the other method to handle uploads. The web server must be configured to handle this. It puts the upload over the URL.

Re: How do I upload a file to a https server.
by eclark (Scribe) on Jun 01, 2004 at 19:31 UTC

    Untested, but try something like this:

    $ua->post($url, Content_Type => 'form-data', Content => [ field1 => 'value1', file => ['data.txt'] ] );

    Put any fields and values you need to post in there, and change file to the name of the upload field in the form.

    I found this info in HTTP::Request::Common

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://358180]
Approved by Paladin
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others perusing the Monastery: (7)
As of 2024-03-28 21:57 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found