Avatar of pavelmed
pavelmed
 asked on

PERL script to upload a file to https site

I have to create a script to automate uploading a file to a secure https site.

I have the URL to login to the site ('https://servername.com/login/home.do#login'), username and password.
While logged in, the site displays an option to select a directory to upload to, a browser button to select a file, and a button upload.

I am trying to automate it using a PERL script but my attempts have been unsuccessful.

Do you know good examples of scripts doing that?
If yes, could you please provide them?


I tried to use a couple of samples I could find, and my code is below
Do you have any suggestions on what should be fixed?

Thank you.

When I ran the script with simply a servername in a https address ('https://servername.com/login/home.do#login'), it returned an error
"500 Can't connect to servername.com:443 (certificate verify failed)"

Then, when I noticed that I was also given a port number (say, 5555), I replaced the URL
with 'https://servername.com:5555/login/home.do#login'
This time the error was different and came after about 30 sec of wait:
500 Can't connect to servername.com:5555 (A connection attempt failed because
 the connected party did not properly respond after a period of time,
or established connection failed because connected host has failed to respond.)

Please note that the port number should NOT be used while calling the login page in the Internet browser.

---------------------------------------------------------------------

# Use HTTP Web Client Library (LWP)
use LWP::UserAgent;
use HTTP::Request;

#from the sample
use HTTP::Cookies;


my $https_login = 'https://servername.com/login/home.do#login';
my $https_post = 'https://servername.com/login/home.do#myFilegateway/Inbox';

my $https_user = 'myUserName';
my $https_pass = 'myPassword';

# create instance of user agent (web browser) session
my $browser = LWP::UserAgent->new();

# need to create temporary "browser" cookie for session state persistence
# cookie stores the session ID used by websites
$browser->cookie_jar( {} );

# Please note: I was also trying to use the following line with the same result:
#$browser->cookie_jar(HTTP::Cookies->new(file => ".cookies.txt",autosave => 1));


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

# Please note: I tried to use each one of the statements below, sending first "user", then trying "userName" (which is a name of
# the UserName control on the web page

#my $response = $browser->post($https_login, [ 'userName' => "$https_user",'password' => "$https_pass" ] );
my $response = $browser->post($https_login, [ 'user' => "$https_user",'password' => "$https_pass" ] );


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


$response = $browser->post($https_post, [filename => ['./theFileNametoUpload.txt'],], );


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

Avatar of undefined
Last Comment
pavelmed

8/22/2022 - Mon
noci

Why perl...?

How about: cURL ( http://curl.haxx.se )
of even libcurl which does have a perl API  but can do all the heavylifting for the HTTP protocol including filling fields etc.
pavelmed

ASKER
PERL is currently present on the box.  The script should run from a UNIX box.
An installation of any new software is not advisable.
PERL can be called from a shell script, that's why PERL is preferable.
Thanks.
ASKER CERTIFIED SOLUTION
noci

Log in or sign up to see answer
Become an EE member today7-DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform
Sign up - Free for 7 days
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.
Not exactly the question you had in mind?
Sign up for an EE membership and get your own personalized solution. With an EE membership, you can ask unlimited troubleshooting, research, or opinion questions.
ask a question
pavelmed

ASKER
Sorry for leaving this question for so long without attention.
I actually found a completely different solution.
But I want to award the only user who responded with all points.
Thank you.
Experts Exchange is like having an extremely knowledgeable team sitting and waiting for your call. Couldn't do my job half as well as I do without it!
James Murphy