Link to home
Start Free TrialLog in
Avatar of George
GeorgeFlag for United States of America

asked on

How to resolve error with LWP UserAgent HTTP GET Request

I need to do an HTTP GET request.

I built a 'tester' program with the following code:

-----------------------------------------------------------------
push @INC, "d:\\InetPub\\wwwroot\\cgibin";
require LWP::Protocol::http;
require LWP::UserAgent;

   my $urlString = "http://www.someserver.com?a=xxx";
    $urlObj = new URI::URL($urlString);
    my $ua = new LWP::UserAgent;
      my $request = new HTTP::Request('GET', $urlObj, undef);
     $request->header('Content-Type', 'text/html');
     $response = $ua->request($request);

---------------------------------------------------------------------

It works fine in my little 'tester' program.
(It does an HTTP GET request, and receives a response from a remote program.)

Then I copied the code into a subroutine of a different program - called 'purchase6'.  
(The top 'push' statement was not in the subroutine, but rather in the main part of the program.)

And when I run it in the purchase6  program, I get this error:

Can't locate object method "init_header" via package "HTTP::Headers" at d:\InetPub\wwwroot\cgibin/HTTP/Message.pm line 189.

The error happens on this statement:
        $response = $ua->request($request);  

I'm really baffled, because the exact same code works in the 'tester' program.

I tried putting the require statements in the main part of the program, just under the 'push' statement, but it did not help.

Also, both the 'tester' program and the 'purchase6'  program reside in the same folder on our server.

I'll appreciate any help you can offer.


                   //email address edited from comment 20739739 by DarthMod - 14/04/09 - 11:25 //

Open in new window

Avatar of Tintin
Tintin

Can you please show the subroutine in purchase6 and the call to it.
Avatar of George

ASKER

I found some more information.
In the purchase6 program, there is a 'use lib' statement that is not in the tester program:

use lib "d:\\InetPub\\wwwroot\\cgibin";

When I add the statement above to the tester program, then the tester program throws the same error.
So it seems that the 'use lib' statement is perhaps causing some confusion as to the methods that are being used.  


Here is the function call:
 
                  my $emUnlockCodeReply = &getEmailMergeUnlockCode($emailmergeQty);
 
Here is the Subroutine:
#---------------------------------------------------------------------------------------------------
#Accepts an integer, containing qty, and returns a string containing an unlock code.
#Places an HTTP GET request to get the unlock code 
sub getEmailMergeUnlockCode{
 
require LWP::Protocol::http;
require LWP::UserAgent;
    
    my $qty = shift; 
    my $testing = 0;  #*****#####******
	my $urlObj;
	my $urlString;
    my $urlString = "http://w3.cg.com/cgibin/EmailMergeTestTarget.html";
    my $queryString;
    my $strAnswer;
    my $email = 'email@somewhere.com';  #for testing
    my $response;
    my $success = 0; # means, able to connect with other server and get a response. 
       
    #We need to strip off first name from last name;
    my $name = $Form{'BillTo_Name'};
    my $firstname;
    my $lastname;
    
    if ($name =~ /^(\w+)\W+(\w+)\.?\W+(.+)/ )  { # eg Zaphoid A. Beetlebrox
      $firstname = $1;  
      $lastname = $3;    
    }elsif ( $name =~ /^(\w+)\W+(.+)/ ) {   # eg Zaphoid Beetlebrox
       $firstname = $1;  
      $lastname = $2;    
 
	 }else{                 # eg:  Beetlebrox
      $firstname = ".";
      $lastname = $name; 
     }
    
    $queryString  = "?initals=$firstname";
    $queryString .= "&name=$lastname";
    $queryString .= "&co_name=$Form{'BillTo_Company'}";       
    $queryString .= "&email=$Form{'Email'}";
    $queryString .= "&qty=$qty";  
    $queryString .= "&pword=xxxxxxx"; 
    if($testing){   
      $queryString .= "\&o_no=TEST"; 
    }else{ 
      $queryString .= "&o_no=$orderID";        
    }
    
    $urlString = "".$urlString.$queryString;  
    
 
    $urlObj = new URI::URL($urlString);
    my $ua = new LWP::UserAgent;
   
     # one way ----
     my $request = new HTTP::Request('GET', $urlObj);
 
 if ($debug){ print "Email Merge Created request object  <br>";}  ### debug 
     
     $request->header('Content-Type', 'text/html');
     
    
     $response = $ua->request($request);
     
  if ($debug){ print "Email Merge Response:  $response <br>";}  ### debug 
 
    if ($response->is_success() ){
       $success = 1;   # other is: is_error();
       $strAnswer = $response->content;
       #if response is error message, handle it after returning string to calling function
       #All error messages returned from API will have 'ERROR' in them.
    }else{
       #Handle the connection error by returning a string containing the word error.
        $strAnswer ="ERROR: Unable to get response from code generator server.";
    }
 
return $strAnswer;	
		
 
}#end

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of Adam314
Adam314

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of George

ASKER

Thanks for you prompt reply.