|
|
|||||||||
|
|||||||||
| |||||||||
|
|
|
| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
#1
|
|||
|
|||
|
Hi All,
I am new to CGI. I code a short program in Perl but it did not work. Could someone tell me what I did wrong? use LWP::UserAgent; use HTTP::Request::Common; $agent = LWP::UserAgent->new; $agent->request(POST 'http://www.mywebsite/cgi-bin/lib/test.cgi', [ To => "123456", Message => "test", ] ) When I run this , nothing happened but if I run an HTML file: <FORM ACTION=htpp://www.mywebsite/cgibin/lib/test.cgi"> To? <INPUT NAME="To" VALUE="123456"> Message? <INPUT NAME=Message VALUE="test"> <P> <INPUT TYPE="submit"> </FORM> the HTML works fine. Any input wold be appreciated. Thanks |
|
#2
|
|||
|
|||
|
You forgot the content_type('application/x-www-form-urlencoded') line.
use LWP::UserAgent; $ua = new LWP::UserAgent; $ua->timeout(30); $ua->agent("AgentName/0.1 " . $ua->agent); my $req = new HTTP::Request POST => 'http://www.mywebsite/cgi-bin/lib/test.cgi'; $req->content_type('application/x-www-form-urlencoded'); $req->content("To=123456&Message=test"); my $res = $ua->request($req); if ($res->is_success) { blah blah } else { blah blah } |
|
#3
|
|||
|
|||
|
To freebsd,
Thanks for your time. I use your code and add the following statement: else { print "BAD" ; print p($req->content); print p(res); } The result is: BAD<P>To=12345&Message=test</P> <P>HTTP::Response=HASH(0x1cb86e8)</P> Any idea?? Thanks a lot. |
|
#4
|
|||
|
|||
|
>>I use your code and add the following statement
No, don't use that. Let me post an example here.. # test.cgi ################################# #!/usr/local/bin/perl read(STDIN, $buffer, $ENV{'CONTENT_LENGTH'}); @pairs = split(/&/, $buffer); foreach $pair (@pairs) { ($name, $value) = split(/=/, $pair); $value =~ tr/+/ /; $value =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C", hex($1))/eg; $value =~ s/<!--(.|n)*-->//g; $value =~ s/<([^>]|n)*>//g; $FORM{$name} = $value; } if (($FORM{'To'} ne "") && ($FORM{'Message'} ne "")) { print "Content-type: text/htmlnn"; print "Ya Made It!n"; } else { print "Content-type: text/htmlnn"; print "Oh No!n"; } ############################################ # script.cgi ############################### #!/usr/local/bin/perl use LWP::UserAgent; my $URL = "http://www.mywebsite/cgi-bin/lib/test.cgi"; my $ua = new LWP::UserAgent; $ua->timeout(30); $ua->agent("AgentName/0.1 " . $ua->agent); my $req = new HTTP::Request POST => $URL; $req->content_type('application/x-www-form-urlencoded'); $req->content("To=123456&Message=test"); my $res = $ua->request($req); if ($res->is_success) { my $content = $res->content; &output_status($content); exit; } else { &output_status("is_success Failed"); exit; } sub output_status { my (@messages) = @_; print "Content-type: text/htmlnn"; my $message; foreach $message (@messages) { print $message; } } ############################################ Note: If is_success okay, it should return the exact output of test.cgi. On the other hand, it should print "is_success Failed" and something screwed up. That is, what you should get is "Ya Made It!" as long as your "To" and "Message" are not blank. If you get "Oh No!", that means either "To" or "Message" has blank value or the variable simply doesn't match. Let me know how it goes. |
|
#5
|
|||
|
|||
|
Hi, again and thanks for your time!
I got the example you posted to work, but when I'm trying the script on any other script (not mine) then I won't work. I tried the script on http://www.arla.se/kokboken/index.asp (just for testing). If I write a HTML-page that sends the variable txtSnabbSok as POST, it works. But if I tries to implement your example on that page it just won't work! Do you have any idea? TIA! This seems to be tricky URL |
|
#6
|
|||
|
|||
|
If you want to automate form submission, do this:
<BLOCKQUOTE><font size="1" face="Verdana,Arial,Helvetica">code:</font><HR><pre> use HTTP::Request::Common "POST"; use LWP::UserAgent; my $ua = new LWP::UserAgent; my $req = POST 'http://whatever.com/mycgi.pl', [ To => '123456', Message => 'test' ]; my $content = $ua->request($req)->as_string; [/code] |
|
#7
|
|||
|
|||
|
Thanks, the code you posted worked for one of my two test pages.
Unfortunetaly, it didn't work for the first page. The script only returned "Object moved". Do you have any idea about how to solve that? However, it worked at all and that's the most important ting. Thank you really much! Merry Christmas, by the way... |
![]() |
| Viewing: Dev Shed Forums > Programming Languages > Perl Programming > How to include parameters in POST |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|