Post to https using perl script

C

cardsharper

Hi

I wanted to post / upload a file to one of our https server

what i am trying to do here is trying to loging to the
https server using the user credentials . then trying to
post a file to another url which in general accepts post
after loging in .

i was able to successfully login, but unable to post
is this something todo with cookies ?

the error its throughing is "302 Found"

------------------here is the code --------------------------

#!/usr/bin/perl
use LWP;
my $url = 'https://192.168.1.3'; # Yes, HTTPS!
my $browser = LWP::UserAgent->new;
my $response = $browser->get($url);
die "Error at $url\n ", $response->status_line, "\n Aborting"
unless $response->is_success;
print "Whee, it worked! I got that ",
$response->content_type, " document!\n";

my $response1 = $browser->post( $url,
[
number => xxxxx,
password => yyyyyyy,
],
);


my $url2 = 'https://192.168.1.3/postfile';

my $response2 = $browser->post ( $url2,
[

filename =>['/tmp/abc.txt'],
],
);

die "Error at $url2\n ", $response2->status_line, "\n Aborting"
unless $response2->is_success;
 
U

usenet

cardsharper said:
i was able to successfully login, but unable to post
is this something todo with cookies ?

If you suspect a problem with cookies, did you read the LWP::UserAgent
docs to see how cookies are handled? It says:
If you site requires cookies, and you don't enable a cookie-jar, how
could that work?

FWIW, most folks would do this type of thing (these days) with
WWW::Mechanize (which is built on LWP but is often preferable - and it
furnishes a cookie-jar (memory resident) by default.
 
C

cardsharper

Well i did look at cookies as soon as i posted so tried the following
unable to see the file uploaded how can i debug this
i am pretty new to http protocol ..

#!/usr/bin/perl

use Data::Dumper;
use LWP::UserAgent;
use HTTP::Cookies;

my $https_login = 'https://192.168.1.3';
my $https_post = 'https://192.168.1.3/post';


my $https_user = 'xxxxx';
my $https_pass = ''yyyyy;

&postHTTPS();


sub postHTTPS
{
#secure login

my $ua = LWP::UserAgent->new();

$ua->protocols_allowed( [ 'https'] );

$ua->cookie_jar(HTTP::Cookies->new(file =>
".cookies.txt",autosave => 1));

my $response = $ua->post($https_login, [ 'user' =>
"$https_user",'password' => "$https_pass", 'switch' => 'Log In' ] );

#print Dumper($response);

if ($response->is_error())
{
printf " %s\n", $response->status_line;
print "https request error!\n";
} else {
my $content = $response->content();
print "$content\n";
}


$response = $ua->post($https_post, [
filename =>['/tmp/abc.txt'],
], );

if ($response->is_error())
{
printf " %s\n", $response->status_line;
print "https request error!\n";
} else {
my $content = $response->content();
print "$content\n";
}
}
 
C

cardsharper

hmmm looked at the webserver log .. and found this
"POST / HTTP/1.1" 302 95 "-" "libwww-perl/5.805"

anyidea where i am going wrong with the code ?
 
S

Sherm Pendley

cardsharper said:
hmmm looked at the webserver log .. and found this
"POST / HTTP/1.1" 302 95 "-" "libwww-perl/5.805"

anyidea where i am going wrong with the code ?

What code? There's no code there, just a line from your web server log.

A line, I should add, that doesn't indicate an error. What makes you think
something might be going wrong?

sherm--
 
U

usenet

cardsharper said:
$response = $ua->post($https_post, [
filename =>['/tmp/abc.txt'],
], );

Time to spend some more time with the docs:

This is the last time I will quote the docs for you (and I've really
done it two times too many). Please RTFM before posting questions in
usenet.

Why aren't you using WWW::Mechanize? Most folks find this module so
much easier for this type of thing...
 
Z

zentara

Hi

I wanted to post / upload a file to one of our https server

what i am trying to do here is trying to loging to the
https server using the user credentials . then trying to
post a file to another url which in general accepts post
after loging in .

i was able to successfully login, but unable to post
is this something todo with cookies ?

the error its throughing is "302 Found"

I don't think it has anything to do with cookies. When
you do a post, you need to setup a request object first.
The login and post can all be done in one step.
Try this, it works for me.

#!/usr/bin/perl
use warnings;
use strict;
use LWP::UserAgent;
use HTTP::Cookies;
use HTTP::Request::Common qw(POST);

my $https_url = 'https://192.168.0.1/cgi-bin/uploads/up1.cgi';

my $https_user = 'xxxxx';
my $https_pass = 'yyyyy';
my $file = 'testout.tgz';

&postHTTPS();

sub postHTTPS
{
my $ua = new LWP::UserAgent;
$ua->protocols_allowed( [ 'https'] );
$ua->cookie_jar(HTTP::Cookies->new(file
=>".cookies.txt",autosave => 1));

#setup request
my $req = POST($https_url,
Content_Type => 'multipart/form-data',

Content =>[
file =>[ $file ],
], );
#setup auth
$req->authorization_basic($https_user, $https_pass);

#do post
my $response = $ua->request($req);
if ($response->is_error())
{
printf " %s\n", $response->status_line;
print "https request error!\n";
} else {
my $content = $response->content();
print "$content\n";
}

if ( $response->is_success ) {
print $response->as_string;
}else {
print $response->status_line;
}

}
__END__
 
J

Juha Laiho

cardsharper said:
I wanted to post / upload a file to one of our https server

what i am trying to do here is trying to loging to the
https server using the user credentials . then trying to
post a file to another url which in general accepts post
after loging in .

i was able to successfully login, but unable to post
is this something todo with cookies ?

the error its throughing is "302 Found"

"302 Found" is not an error. It is a response status (error codes
are also reponse status codes, but not all response status codes
are error codes).

Here's what HTTP documentation (IETF RFC 2616) says about 302:

10.3.3 302 Found

The requested resource resides temporarily under a different URI.
Since the redirection might be altered on occasion, the client SHOULD
continue to use the Request-URI for future requests. This response is
only cacheable if indicated by a Cache-Control or Expires header field.

The temporary URI SHOULD be given by the Location field in the
response. Unless the request method was HEAD, the entity of the
response SHOULD contain a short hypertext note with a hyperlink to
the new URI(s).

If the 302 status code is received in response to a request other than
GET or HEAD, the user agent MUST NOT automatically redirect the request
unless it can be confirmed by the user, since this might change the
conditions under which the request was issued.
Note: RFC 1945 and RFC 2068 specify that the client is not allowed
to change the method on the redirected request. However, most
existing user agent implementations treat 302 as if it were a 303
response, performing a GET on the Location field-value regardless
of the original request method. The status codes 303 and 307 have
been added for servers that wish to make unambiguously clear which
kind of reaction is expected of the client.

This is what is commonly called a "redirect". You're given the address
at which the requested resource actually resides, and your code should
handle the access to the correct resource.

You seem to be unsure about cookies, and you should be. If the site
uses cookies for access control, your code should also handle cookies.
Upon login (depending on the login method, though) the server creates
a session, and will place the session identifier into a cookie. To
be associated to that same session (and thus, to be an authenticated
request), all requests from the same client must contain the provided
session cookie.

In the above, the redirect may well point to the login page (as it seems
your code is not passing any cookie).
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,020
Latest member
GenesisGai

Latest Threads

Top