#1
  1. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2000
    Location
    Falls Church VA 22031
    Posts
    3
    Rep 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
  2. #2
  3. No Profile Picture
    freebsd
    Guest
    Devshed Newbie (0 - 499 posts)
    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
    }
  4. #3
  5. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2000
    Location
    Falls Church VA 22031
    Posts
    3
    Rep 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.
  6. #4
  7. No Profile Picture
    freebsd
    Guest
    Devshed Newbie (0 - 499 posts)
    >>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.
  8. #5
  9. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2000
    Posts
    4
    Rep Power
    0
    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
  10. #6
  11. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2000
    Posts
    0
    Rep 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]
  12. #7
  13. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2000
    Posts
    4
    Rep Power
    0
    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...

IMN logo majestic logo threadwatch logo seochat tools logo