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 = "";
    $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/ 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

Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Can you please show the subroutine in purchase6 and the call to it.
GeorgeAuthor Commented:
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 = "";
    my $queryString;
    my $strAnswer;
    my $email = '';  #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"; 
      $queryString .= "\&o_no=TEST"; 
      $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.
       #Handle the connection error by returning a string containing the word error.
        $strAnswer ="ERROR: Unable to get response from code generator server.";
return $strAnswer;	

Open in new window

It sounds like there is a version of some module in the d:\InetPub\wwwroot\cgibin directory that is causing problems.  Try change the "require LWP..." statements to "use LWP...", and put them before the "use lib" statement:

#!/usr/bin/perl             (or your path)
use strict;                   (you should already have this... )
use LWP::Protocol::http;
use LWP::UserAgent;
use lib "d:\\InetPub\\wwwroot\\cgibin";
sub getEmailMergeUnlockCode {

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
GeorgeAuthor Commented:
Thanks for you prompt reply.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.