Perl Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me
Go Back   Dev Shed ForumsProgramming LanguagesPerl Programming

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old August 31st, 2000, 11:33 AM
Howard Dean Howard Dean is offline
Junior Member
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Aug 2000
Location: Falls Church VA 22031
Posts: 3 Howard Dean User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
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

Reply With Quote
  #2  
Old August 31st, 2000, 04:08 PM
freebsd
Guest
Dev Shed Newbie (0 - 499 posts)
 
Posts: n/a  
Time spent in forums:
Reputation Power:
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
}

Reply With Quote
  #3  
Old September 1st, 2000, 10:16 AM
Howard Dean Howard Dean is offline
Junior Member
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Aug 2000
Location: Falls Church VA 22031
Posts: 3 Howard Dean User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
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.

Reply With Quote
  #4  
Old September 1st, 2000, 05:31 PM
freebsd
Guest
Dev Shed Newbie (0 - 499 posts)
 
Posts: n/a  
Time spent in forums:
Reputation Power:
>>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.

Reply With Quote
  #5  
Old December 17th, 2000, 03:04 PM
udd udd is offline
Junior Member
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Oct 2000
Posts: 4 udd User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Send a message via ICQ to udd
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

Reply With Quote
  #6  
Old December 17th, 2000, 04:15 PM
Anonym0us Anonym0us is offline
Junior Member
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Dec 2000
Posts: 0 Anonym0us User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
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]

Reply With Quote
  #7  
Old December 18th, 2000, 01:55 PM
udd udd is offline
Junior Member
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Oct 2000
Posts: 4 udd User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Send a message via ICQ to udd
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...

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesPerl Programming > How to include parameters in POST


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 5 hosted by Hostway
Stay green...Green IT