http://www.perlmonks.org?node_id=31964

Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

The html is in the follwing format:
<html> <head> <title>someTitle</title> </head> <form action="../cgiDir/go.pl" method=POST> <input type=hidden name="a" value="aswdc"> <input type=hidden name="b" value="4"> <input type="submit" name="B1" value="Submit"> </form> </body> </html>
The cgi is in the following format:
#usr/etc. use CGI; use HTTP::Request::Common; use LWP::UserAgent; $query = new CGI; $query->use_named_parameters(1); $var1 = $query->param('a'); $var2 = $query->param('b'); $ua = new LWP::UserAgent; $req=POST 'http://www.someqURL.com',[v1=>$var1,v2=>$var2]; print $ua->request($req)->as_string; exit 0;
I keep on getting a Server error. What am I doing wrong? Thanks in advance.

Originally posted as a Categorized Question.

Replies are listed 'Best First'.
Re: How do post to a URL from a form and display the results in a browser?
by icuc (Initiate) on Sep 13, 2000 at 07:15 UTC
    I think this , perhaps , could satisfy your need.
    ---------------------------

    #!/user/local/bin/perl

    use strict;
    use CGI;
    use HTTP::Request;
    use LWP::UserAgent;

    my $q = CGI->new();
    $q->use_named_paramters(1);
    my $var1 = $q->param('a');
    my $var2 = $q->param('b');

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

    my $method = "POST";
    my $url = "http://where_you_want";

    use HTTP::Header;
    my $header = HTTP::Header->new();
    my $content = "v1=".$var1."&v2=".$var2;
    my $request = HTTP::Request->new($method, $url, $header, $content);

    use HTTP::Response;
    my $response = $ua->request($request);
    if($response->is_success) {
    print $response->content;
    }
    else {
    print $response->error_as_HTML;
    }
    -------------------------------------
    for more details, please check Perl's Documents HTTP::Request, HTTP::Header, HTTP::Response, LWP::UserAgent, etc.
Re: How do post to a URL from a form and display the results in a browser?
by rodry (Beadle) on Sep 12, 2000 at 00:14 UTC
    Not that I am an expert on this PERL thing, but in my experience a server error is simply a generic error message the web server generates when a script craps out.

    What I would do is check the apache logs and see exactly what the error could be.

    By the way, did you try the script from the console?

Re: How do post to a URL from a form and display the results in a browser?
by icuc (Initiate) on Sep 14, 2000 at 06:03 UTC
    yes, I run script from console!!
    a server error? 502? or other?
Re: How do post to a URL from a form and display the results in a browser?
by merlyn (Sage) on Sep 12, 2000 at 00:43 UTC
    If you're running this as a CGI program (as your use CGI would imply), I fail to see where you are printing the appropriate CGI header. Add
    print $query->header, $query->start_html("some title");
    before the rest of your program.

    And get a monkname! They're free, or rather, at least half price this week! It's hard to be motivated to answer a transient. {grin}