Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical
 
PerlMonks  

How do post to a URL from a form and display the results in a browser?

( #31964=categorized question: print w/ replies, xml ) Need Help??
Contributed by Anonymous Monk on Sep 12, 2000 at 00:00 UTC
Q&A  > CGI programming


Description:

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.

Answer: How do post to a URL from a form and display the results in a browser?
contributed by icuc

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.

Answer: How do post to a URL from a form and display the results in a browser?
contributed by rodry

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?

Answer: How do post to a URL from a form and display the results in a browser?
contributed by icuc

yes, I run script from console!!
a server error? 502? or other?

Answer: How do post to a URL from a form and display the results in a browser?
contributed by merlyn

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}

Please (register and) log in if you wish to add an answer



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.
  • Log In?
    Username:
    Password:

    What's my password?
    Create A New User
    Chatterbox?
    and the web crawler heard nothing...

    How do I use this? | Other CB clients
    Other Users?
    Others wandering the Monastery: (13)
    As of 2015-07-31 12:29 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?

      The top three priorities of my open tasks are (in descending order of likelihood to be worked on) ...









      Results (277 votes), past polls