Solved

POST action

Posted on 2002-05-29
7
177 Views
Last Modified: 2010-03-05
Instead of going to a website, type in with some info, and click on send button, is it possible for me to write a script such that it'll post the data the same way the webpage does it?

Right now, I have a cgi perl script that accepts input from a webpage.  It accepts 2 fields that will be used in the perl script.

Please advise.
yam
0
Comment
Question by:YamSeng
7 Comments
 
LVL 51

Expert Comment

by:ahoffmann
ID: 7044170
0
 
LVL 1

Expert Comment

by:GorGor1
ID: 7044450
Note that that doesn't always work.  I have code in my perl scripts that will not allow someone to include the parameters directly in the URL.
0
 
LVL 4

Accepted Solution

by:
dda earned 50 total points
ID: 7046486
use HTML::Form;

my $form = HTML::Form->parse($html,$url);
$form->value('login','yourlogin');
$form->value('password','yourpass');
$form->value('action','login');
my $req = $form->click;

etc., etc.

Hope this helps.
0
PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

 
LVL 1

Author Comment

by:YamSeng
ID: 7046537
ahoffmann, where should I put that line in?  a csh script?  or perl script? just that 1 line?

dda, I'll try that out.
From my understanding, you are trying to fill in the details for the form I should have online, and clicking the submit button. (correct me if I'm wrong).
0
 
LVL 4

Expert Comment

by:dda
ID: 7046550
Yes, correct. You will need to use other modules as well:

use HTML::Form;
use LWP;

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

my $url = 'http://somewhere.com/script.cgi'; #
my $login_req = HTTP::Request->new(GET => $url);
my $login_res = $ua->request($login_req);
my $html = $login_res->content;

my $form = HTML::Form->parse($html,$url);
$form->value('login','yourlogin');
$form->value('password','yourpass');
$form->value('action','login');
my $req = $form->click;

my $res = $ua->request($req);
0
 
LVL 51

Expert Comment

by:ahoffmann
ID: 7049734
> .. put that line in?  a csh script?  or perl script?
you said you still have a perl script, so that script would be the best place 'cause you still have variable names and values there too.
Use the example suggested by dda.
If you have wget you also might use:
  system("wget http://www.som.dom/path/to/your.cgi?name-of-field1=value-of-field1&name-of-field2=value-of-field2 ");

not as elegant as LWP, but quick&dirty ;-)
0
 
LVL 8

Expert Comment

by:jhurst
ID: 7065426
As I read the question all the answerers so far are missing something.  Buy maybe it is me.  If you actually want your script, not another page, to do the post then this is what you need to do, and forget extra pages.

I have a sub that I use as follows:
sub doAPost {                   #post to _[0], the data in $_[1]

  # Create a user agent object
  use LWP::UserAgent;

  $hdrs=new HTTP::Headers(User_Agent => 'ResourceDataBrowser',
  Referer=>"http://$ENV{HTTP_HOST}");

  $ua = LWP::UserAgent->new;

  # Create a request
  my $req = HTTP::Request->new(POST => $_[0],$hdrs);
  $req->content_type('application/x-www-form-urlencoded');
  $req->content($_[1]);

  # Pass request to the user agent and get a response back
  my $res = $ua->request($req);

  # Check the outcome of the response
  if ($res->is_success) {
    return $res->content;
    }
  else {
    return -1;
    }
  }


0

Featured Post

ScreenConnect 6.0 Free Trial

Explore all the enhancements in one game-changing release, ScreenConnect 6.0, based on partner feedback. New features include a redesigned UI, app configurations and chat acknowledgement to improve customer engagement!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

I've just discovered very important differences between Windows an Unix formats in Perl,at least 5.xx.. MOST IMPORTANT: Use Unix file format while saving Your script. otherwise it will have ^M s or smth likely weird in the EOL, Then DO NOT use m…
A year or so back I was asked to have a play with MongoDB; within half an hour I had downloaded (http://www.mongodb.org/downloads),  installed and started the daemon, and had a console window open. After an hour or two of playing at the command …
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…

803 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question