Solved

POST action

Posted on 2002-05-29
7
159 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
Comment Utility
0
 
LVL 1

Expert Comment

by:GorGor1
Comment Utility
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
Comment Utility
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
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
LVL 1

Author Comment

by:YamSeng
Comment Utility
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
Comment Utility
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
Comment Utility
> .. 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
Comment Utility
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

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Many time we need to work with multiple files all together. If its windows system then we can use some GUI based editor to accomplish our task. But what if you are on putty or have only CLI(Command Line Interface) as an option to  edit your files. I…
In the distant past (last year) I hacked together a little toy that would allow a couple of Manager types to query, preview, and extract data from a number of MongoDB instances, to their tool of choice: Excel (http://dilbert.com/strips/comic/2007-08…
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…
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.

772 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

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now