What I Learned Today      [ New Post ]

Friday, February 04, 2005

Posting a form through a CGI script

The problem:
I have a web page that needs to post a form. But the form needs to be processed by an intermediate CGI script before it is submitted to the final CGI script. So kinda like this:
WEB_PAGE ----> CGI_SCRIPT_1 ----> CGI_SCRIPT_2
The problem lies with the first script. How do you get that script to transmit data to the second script?

The solution:
Use Perl's LWP module like so:
use LWP;
  my $browser = LWP::UserAgent->new;
  my $url = 'http://something';
  my $response = $browser->post (
    "http://something.com",
    [
      'formkey1' => "value 1",
      'formkey2' => "value 2"
    ]
  );
  die "$url error: ", $response->status_line
    unless $response->is_success;
  die "Weird content type at $url -- ", $response->content_type
    unless $response->content_type eq 'text/html';
  print "Response from remote script:\n" . $response->content;

Code ripped shameless from somewhere on perl.com

0 Comments:

Post a Comment

<< Home