9 Replies Latest reply on Sep 8, 2005 8:18 AM by 807573

    using perl with the HTTP POST method to exchange XML with the Auth service

    807573
      Has anyone written a perl script to do this yet?

      I've been trying all day and keep getting "Content is not allowed in prolog.

      I've used the example xml from the docs
      <?xml version="1.0" encoding="UTF-8"?>

      <AuthContext version="1.0">

      <Request authIdentifier="0">

      <NewAuthContext orgName="dc=exampleorg,dc=com">

      </NewAuthContext>

      </Request>

      </AuthContext>

      Then I read the python post at http://swforum.sun.com/jive/thread.jspa?forumID=79&threadID=22370

      and the error changed to Premature end of file.

      this is the portion of perl that is getting the error:

      use strict;

      use LWP::UserAgent;
      use HTTP::Request::Common;

      my $browser = LWP::UserAgent->new(agent => 'xml client');


      my $greeting = <<GREETING;
      <?xml version="1.0" encoding="UTF-8" standalone="yes"?>
      <RequestSet vers="1.0" svcid="auth" reqid="1">
           <Request>
           <![CDATA[<?xml version="1.0" encoding="UTF-8"?>
           <AuthContext version="1.0">
                <Request authIdentifier="0">
                     <NewAuthContext orgName="dc=belo,dc=com">
                     </NewAuthContext>
                </Request>
           </AuthContext>]]></Request>
      </RequestSet>
           
      GREETING



      my $response = $browser->request(POST 'http://idpoc1.test.belo.com/amserver/authservice',
                          Content_Type => 'text/xml',
                          Content     => [xmlRequest => $greeting]
                          );
      print $response->error_as_HTML unless $response->is_success;
        • 1. Re: using perl with the HTTP POST method to exchange XML with the Auth service
          807573
          Try this script :
          use strict;
          
          use LWP::UserAgent;
          use HTTP::Request;
          
          my $ua = LWP::UserAgent->new(env_proxy => 1, keep_alive => 1, timeout => 30, ); 
          
          my @greeting = <>;     #-- Read file containing XML struct to send
          my $data_to_send;     #-- And build a string of it
          foreach my $newItem (@greeting) {
               $data_to_send = "$data_to_send$newItem";
          }
          
          print "Sending :\n$data_to_send\n";
          
          #-- Create the Request Object and send the data
          my $response = 
               $ua->request(HTTP::Request->new('GET', 'http://idpoc1.test.belo.com/amserver/authservice', 
               HTTP::Headers->new(Content_Type => "text/xml"), $data_to_send));
          
          #-- Print the response
          print $response->as_string;
          This will help you avoid the problem with Content in prolog, the AppServer will read the raw XML which you set up either in you script, or read from a file...
          • 2. Re: using perl with the HTTP POST method to exchange XML with the Auth service
            807573
            I am trying to do a HTTP post of a xml document. This codes works perfect for me. But I also need to attach a ID to the data I am sending. So that once the server gets the data and posts it, they can query using this ID. I was told to use a name value pair. Ex. if the ID I want to use is "sample" and my data is in the variable $data_to_send, I need to say somthing like sample=$data_to_send. But I don't know how.
            Any help is appreciated.

            Thanks in advance.
            • 3. Re: using perl with the HTTP POST method to exchange XML with the Auth service
              807573
              Not quite sure what you want to do, but it seems like you are going to try sending a "name=value" pair instead of pure raw xml. This could easily be achieved by adding more information to the string @data_to_send, like this :
              #-- Build the HTTP Request
              my $req = HTTP::Request->new('POST', $url, $head, "sample=$data_to_send");
              If you need to send an arbitrary ID which is really not part of the XML to post, you may also append special HTTP Header values, like this:
              my $head = HTTP::Headers->new(Content_Type => "text/xml", "ID" => "sample");
              Then the server can pick up the value related to the field ID in the header elements.
              Hope this is of some help.
              • 4. Re: using perl with the HTTP POST method to exchange XML with the Auth service
                807573
                I have been trying to do HTTP POST as a name =value pair according to the code mentioned over here,I am facing an error as :

                The specified CGI application misbehaved by not returning a complete set of HTTP headers. The headers it did return are:


                Can't locate object method "Request=HASH(0x1bad9c8)" via package "HTTP" at c:\inetpub\scripts\posting.pl line 33, line 3.

                where posting is my perl file
                Can any one help me with this name=value pair.

                thanx in advance
                • 5. Re: using perl with the HTTP POST method to exchange XML with the Auth service
                  807573
                  Looks like you are building the Request object incorrectly. If you could post the code and sample data of what you're sending, then it's easier to give you a better answer.
                  • 6. Re: using perl with the HTTP POST method to exchange XML with the Auth service
                    807573
                    this is the code i am using for posting my XML data:

                    use LWP::UserAgent;
                    use HTTP::Request;

                    use IO;
                    use XML::Writer;

                    my $ua = LWP::UserAgent->new(env_proxy => 1, keep_alive => 1, timeout => 30, );
                    open INPUT, "<F:\AcceptIncident.xml";
                    print "<HTML>\n";
                    print "<BODY>\n";
                    print "<H4>Hello World</H4>\n";
                    my @greeting = <INPUT>; #-- Read file containing XML struct to send
                    print "<H4>Hello World</H4>\n";
                    my $data_to_send; #-- And build a string of it
                    foreach my $newItem (@greeting) {
                    $data_to_send = "$data_to_send$newItem";
                    }
                    print "Sending \n";
                    print "<H4>Hello World</H4>\n";


                    my $head = HTTP::Headers->new(Content_Type => "text/xml", "ID" => "sample");
                    my $req = HTTP::Request->new('POST', 'http://hareesh/scripts/test1.pl', $head, "sample=$data_to_send");
                    my $response = $ua->request($req);
                    print $response->as_string;


                    and this is test1.pl :

                    #!/usr/bin/perl
                    use XML::Simple;
                    use Data::Dumper;


                    print qq{Content-type: text/xml\n\n};

                    print "<HTML>\n";
                    print "<BODY>\n";
                    print "<H4>Hello World</H4>\n";
                    print "<H4>Hello World</H4>\n";


                    if($ENV{'REQUEST_METHOD'} eq "GET"){
                    $my_data = $ENV{'QUERY_STRING'};
                    print "jfghdsfjghsdg$my_data";
                    }
                    else {
                    $data_length = $ENV{'CONTENT_LENGTH'};
                    $bytes_read = read(STDIN, $my_data, $data_length);
                    **print "$bytes_read,$my_data";
                         $xml = new XML::Simple (KeyAttr=>[]);
                         $data = $xml->XMLin("$my_data");
                    print "\n <H4>Helloooooooo World</H4>\n";
                    print "i want to be here \n";
                    $transcode=$data->{txnInfo}->{sourcePartnerCode};
                    #print "$data->{txnInfo}->{sourcePartnerCode}";
                    print $transcode;
                         CASE: {
                              ($transcode==6) && do {
                              print "i am here";
                              $lstmoddat=$data->{txnInfo}->{txnDate};
                              $srctktid=$data->{unitOfWorkInfo}->{sourceTicketID};

                              $actvtylogdesc=$data->{unitOfWorkInfo}->{diaryEntry};
                              $status=$data->{unitOfWorkInfo}->{Status};
                              $command='bop_cmd -f update1.frg "upd_stats("""Administrator""","""'.$srctktid.'""","""FIP""","""what is this???""")"';
                              print "$lstmoddat $srctktid $status \n";
                              print "$command";
                              system("$command");
                              print "i am done";
                              last CASE;
                         };

                    };
                    }

                    print "\n <H4>Hello World</H4>\n";



                    print "</BODY>\n";
                    print "</HTML>\n";

                    it is able to execute until ** line but it doesn't go beyond that i am unable to figure out is it the correct way of posting and capturing as "name=value" pair. any suggestions are welcome.

                    and thanks for responding marvik.
                    • 7. Re: using perl with the HTTP POST method to exchange XML with the Auth service
                      807573
                      hey sorry for that jumbled up code i forgot that it has some HTML tags in it any way my code for posting is:


                      use LWP::UserAgent;
                      use HTTP::Request;

                      use IO;
                      use XML::Writer;

                      my $ua = LWP::UserAgent->new(env_proxy => 1, keep_alive => 1, timeout => 30, );
                      open INPUT, "<F:\AcceptIncident.xml";
                      my @greeting = <INPUT>; #-- Read file containing XML struct to send
                      print "<H4>Hello World</H4>\n";
                      my $data_to_send; #-- And build a string of it
                      foreach my $newItem (@greeting) {
                      $data_to_send = "$data_to_send$newItem";
                      }
                      print "Sending \n";



                      my $head = HTTP::Headers->new(Content_Type => "text/xml", "ID" => "sample");
                      my $req = HTTP::Request->new('POST', 'http://hareesh/scripts/test1.pl', $head, "sample=$data_to_send");
                      my $response = $ua->request($req);
                      print $response->as_string;


                      and my tes1.pl is:

                      #!/usr/bin/perl
                      use XML::Simple;
                      use Data::Dumper;


                      print qq{Content-type: text/xml\n\n};


                      if($ENV{'REQUEST_METHOD'} eq "GET"){
                      $my_data = $ENV{'QUERY_STRING'};
                      print "jfghdsfjghsdg$my_data";
                      }
                      else {
                      $data_length = $ENV{'CONTENT_LENGTH'};
                      $bytes_read = read(STDIN, $my_data, $data_length);
                      *** print "$bytes_read,$my_data";
                           $xml = new XML::Simple (KeyAttr=>[]);
                           $data = $xml->XMLin("$my_data");

                      print "i want to be here \n";
                      $transcode=$data->{txnInfo}->{sourcePartnerCode};
                      #print "$data->{txnInfo}->{sourcePartnerCode}";
                      print $transcode;
                           CASE: {
                                ($transcode==6) && do {
                                print "i am here";
                                $lstmoddat=$data->{txnInfo}->{txnDate};
                                $srctktid=$data->{unitOfWorkInfo}->{sourceTicketID};

                                $actvtylogdesc=$data->{unitOfWorkInfo}->{diaryEntry};
                                $status=$data->{unitOfWorkInfo}->{Status};
                                $command='bop_cmd -f update1.frg "upd_stats("""Administrator""","""'.$srctktid.'""","""FIP""","""what is this???""")"';
                                print "$lstmoddat $srctktid $status \n";
                                print "$command";
                                system("$command");
                                print "i am done";
                                last CASE;
                           };

                      };
                      }

                      my posting does work well till *** of the test1.pl but it doesn't go beyond it. i am not able to figure out if it is the correct way of posting and capturing XML data as "name=value" pair.
                      • 8. Re: using perl with the HTTP POST method to exchange XML with the Auth service
                        807573
                        Your problem seems to lie in the build of the request. Here you use the "sample=$data" which will invalidate the XML itself. I've tested your code changing only the build of the Request and this works fine! Given an XML like this :
                        <?xml version="1.0" encoding="ISO-8859-1"?>
                        <data>
                             <txnInfo sourcePartnerCode="6" txnDate="2005.09.08"/>
                             <unitOfWorkInfo sourceTicketID="SourceTicket_1" diaryEntry="Diray_1" Status="Status_OK"/>
                        </data>
                        The Server Stub should look like this :
                        #!/usr/bin/perl 
                        use XML::Simple; 
                        use Data::Dumper; 
                        print qq{Content-type: text/xml\n\n}; 
                        if($ENV{'REQUEST_METHOD'} eq "GET"){ 
                             $my_data = $ENV{'QUERY_STRING'}; 
                             print "jfghdsfjghsdg$my_data"; 
                        } else { 
                             $data_length = $ENV{'CONTENT_LENGTH'}; 
                             $bytes_read = read(STDIN, $my_data, $data_length);
                             print "$bytes_read, $my_data"; 
                             $xml = new XML::Simple (KeyAttr=>[]);
                             print "have passed XML::Simple creation\n";
                             $data = $xml->XMLin("$my_data");
                             print "i want to be here \n"; 
                             $transcode=$data->{txnInfo}->{sourcePartnerCode}; 
                             #print "$data->{txnInfo}->{sourcePartnerCode}"; 
                             print $transcode; 
                             CASE: { 
                                  ($transcode==6) && do { 
                                       print "i am here"; 
                                       $lstmoddat=$data->{txnInfo}->{txnDate}; 
                                       $srctktid=$data->{unitOfWorkInfo}->{sourceTicketID}; 
                                       $actvtylogdesc=$data->{unitOfWorkInfo}->{diaryEntry}; 
                                       $status=$data->{unitOfWorkInfo}->{Status}; 
                                       $command='bop_cmd -f update1.frg "upd_stats("""Administrator""","""'.$srctktid.'""","""FIP""","""what is this???""")"'; 
                                       print "$lstmoddat $srctktid $status \n"; 
                                       print "$command"; 
                                       system("$command"); 
                                       print "i am done"; 
                                       last CASE; 
                                  }; 
                             }; 
                        } 
                        And the client stub should look like this :
                        use LWP::UserAgent; 
                        use HTTP::Request; 
                        use IO; 
                        #use XML::Writer; 
                        my $ua = LWP::UserAgent->new(env_proxy => 1, keep_alive => 1, timeout => 30, ); 
                        open INPUT, "<AcceptIncident.xml"; 
                        my @greeting = <INPUT>; #-- Read file containing XML struct to send 
                        print "Hello World\n"; 
                        my $data_to_send; #-- And build a string of it 
                        foreach my $newItem (@greeting) { 
                             $data_to_send = "$data_to_send$newItem"; 
                        } 
                        print "Sending \n"; 
                        my $head = HTTP::Headers->new(Content_Type => "text/xml", "ID" => "sample"); 
                        my $req = HTTP::Request->new('POST', 'http://matrix/cgi-bin/test1.pl', $head, "$data_to_send"); 
                        my $response = $ua->request($req); 
                        print $response->as_string; 
                        Also you should make sure you have both HTTP::Request amd XML::Simple installed, orelse this will not work.

                        Hope this is of some help to you.
                        • 9. Re: using perl with the HTTP POST method to exchange XML with the Auth service
                          807573
                          hey thanx marvik, it indeed work for me. but i still couldn't get the funda of using a "name=value " pair if we are not assigning something to that pair. if you have time you can make it clear for me.

                          thanx a lot.