Solved

POST action

Posted on 2002-05-29
7
198 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
7 Comments
 
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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

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…
There are many situations when we need to display the data in sorted order. For example: Student details by name or by rank or by total marks etc. If you are working on data driven based projects then you will use sorting techniques very frequently.…
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…
Six Sigma Control Plans

615 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