Script calling CGI

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • palm
    New Member
    • Feb 2007
    • 27

    #1

    Script calling CGI

    Hi There,
    I need to send some parameters from a Perl program to another Perl program that's in cgi-bin. I'm trying to do an http request, but it's not working properly. Any ideas?

    Palm.
  • KevinADC
    Recognized Expert Specialist
    • Jan 2007
    • 4092

    #2
    No ideas without seeing your code or explaining what "not working properly" means.

    Comment

    • palm
      New Member
      • Feb 2007
      • 27

      #3
      Hi there,
      I've a perl program called send.pl in a unix directory (in /usr/main/laal/work) and another perl program called receive.pl on the web server (in /usr/docs/www/cgi-bin). I need to send some values from send.pl to receive.pl. I like to use http request to call receive.pl and send the values something like this:

      http://abcd.com/cgi-bin/receive.pl?tota l=789456&name=j ack&computer=de sktop

      I'm a new programmer and I've no idea about how to create http request and send the values to the other program. Could any one please help me out?

      Thank You.

      Palm.

      Comment

      • miller
        Recognized Expert Top Contributor
        • Oct 2006
        • 1086

        #4
        Hello palm,

        That was much more clear.

        Ok, it sounds like all you need is an introduction to LWP. This is a set of cpan modules typically used to process web pages. You can do a lot of things with them, so take a moment to browse around all the other modules in the package at some point. But for now, I would simply point you to this:

        cpan LWP::Simple

        If you need help installing this module, look at cpanfaq.

        cpanfaq How to install perl modules

        - Miller

        Comment

        • KevinADC
          Recognized Expert Specialist
          • Jan 2007
          • 4092

          #5
          Ditto Miller :)

          Comment

          • palm
            New Member
            • Feb 2007
            • 27

            #6
            Hi Kevin & Miller,
            I'm a new programer so I'm having little hard time in getting through this. I wrote this code and tried to send the parameters but i'm getting some message like The requested document was not found, etc etc.. This is what I've written:

            Code:
            #! /usr/bin/perl -w
            use strict;
            
             use LWP::UserAgent;
             my $ua = LWP::UserAgent->new;
              my $req = HTTP::Request->new(POST => 'http://abcd.com/cgi-bin/receive.pl');
              my $res = $ua->request($req);
              print $res->as_string;
            how can I send the parameters to receive.pl from this code(parameters : total, name, computer, etc)?. I'm totally lost....

            Palm.

            Comment

            • miller
              Recognized Expert Top Contributor
              • Oct 2006
              • 1086

              #7
              Hi Palm,

              You're almost there. This is how I would suggest that you do it:

              Code:
              #! /usr/bin/perl -w
              use strict;
              
              use HTTP::Request::Common qw(POST);
              use LWP::UserAgent;
              
              my $ua = new LWP::UserAgent;
              $ua->agent("send.pl/1.0");
              
              # Create HTTP POST request
              my $req = POST "http://abcd.com/cgi-bin/receive.pl", [
              	total		=> 789456,
              	name		=> 'jack',
              	computer	=> 'desktop',
              ];
              my $res = $ua->request($req);
              
              if($res->is_success) {
              	print "Content = '" . $res->content . "'";
              } else {
              	print "Error = '" . $res->status_line . "'";
              }
              Note: This code is untested.

              - Miller

              Comment

              • miller
                Recognized Expert Top Contributor
                • Oct 2006
                • 1086

                #8
                You can find documentation of this method of posting at the following locations:

                cpan LWP::UserAgent #Request Methods
                cpan lwpcook #POST

                - Miller

                Comment

                • palm
                  New Member
                  • Feb 2007
                  • 27

                  #9
                  Hi Miller,
                  Thanks a lot for your help. I tried those steps and getting '404 not found' error message. I used those steps in my send.pl and I wrote the following code for receive.pl. I'm not sure where the problem is:

                  #! /usr/bin/perl -w
                  use strict;
                  use LWP::UserAgent;
                  use HTTP::Request:: Common;
                  use URI;

                  my $ua = LWP::UserAgent->new;
                  my $uri = URI->new('http://abcd.com/cgi-bin/receive.pl');

                  my $response = $ua->simple_request (GET $uri);
                  print $response->content;


                  I really appreciate your help. Please let me know if you've any idea.

                  Palm.

                  Comment

                  • palm
                    New Member
                    • Feb 2007
                    • 27

                    #10
                    http request help

                    Hi There,
                    I've a perl program called "receive.pl ", which is supposed to get some variables from another program called "send.pl". When I run my receive.pl, I'm getting page not found error message. Since I'm new to programming, it'll be great if any one could help me out. This is how my receive.pl looks like:

                    Code:
                    #! /usr/bin/perl 
                    use strict;
                    
                    use LWP::UserAgent;
                    use HTTP::Request::Common;
                    use URI;
                    
                    my $ua = LWP::UserAgent->new;
                    my $uri = URI->new('http://abcd.com/cgi-bin/receive.pl');
                    my $response = $ua->simple_request(GET $uri);
                    print $response->content;
                    I've doubt in this line:
                    my $uri = URI->new('http://abcd.com/cgi-bin/receive.pl');

                    My current program is called receive.pl and i'm creating new URI using this name. I'm lost here. Can anyone please help me?

                    Thank You.

                    Palm.

                    Comment

                    • KevinADC
                      Recognized Expert Specialist
                      • Jan 2007
                      • 4092

                      #11
                      seems you already asked question this in your other thread.

                      Comment

                      • miller
                        Recognized Expert Top Contributor
                        • Oct 2006
                        • 1086

                        #12
                        Hi palm,

                        Please do not double post your threads. Instead, continue in the thread you've already started for the problem.

                        I'm merging the threads now.

                        - Miller

                        Comment

                        • palm
                          New Member
                          • Feb 2007
                          • 27

                          #13
                          Sorry Miller,
                          I couldn't find a solution so I thought I better divide the probem into small parts and start a new discussion. I'll continue posting in this old topic then.

                          Thankz.

                          Palm.

                          Comment

                          • palm
                            New Member
                            • Feb 2007
                            • 27

                            #14
                            Hi there,
                            Now both programs are working with out any errors, but I don't see/get the variables (total=789456,n ame='jack',comp uter= 'desktop'). How can I get them in receive.pl? response-> content doesn't seem working.

                            Palm.

                            Comment

                            • palm
                              New Member
                              • Feb 2007
                              • 27

                              #15
                              Hi There,
                              I tried to get/print the variables in the http request like this:

                              use CGI qw/:standard/;
                              my $cgi = new CGI;
                              ..
                              ..
                              ..

                              print $cgi->param("total") ;
                              print $cgi->param("name" );
                              print $cgi->param("compute r");

                              but it's not printing anything. I'm totally lost. I'm I missing anythig? Please some one help me with this problem.


                              Palm.

                              Comment

                              Working...