in reply to Cannot access HTTP::Response content properly

Is there a way you can post your code so that we can simulate the problem? I can test your code on my machine and if I am able to print it to a file, then at least it will narrow down your problem.
  • Comment on Re: Cannot access HTTP::Response content properly

Replies are listed 'Best First'.
Re^2: Cannot access HTTP::Response content properly
by URAvgDeveloper101 (Novice) on Nov 03, 2009 at 00:39 UTC

    All, Here is my code:

    #!/usr/bin/perl use strict; use LWP::UserAgent; my $ua; my $req; my $res; $ua = new LWP::UserAgent; $ua->agent("AgentName/0.1 " . $ua->agent); #REQUEST $req = HTTP::Request->new(GET =>'http://SOMEURLHERE'); #PASS REQUEST $res = $ua->request($req); #GET RESULTS if($res->is_success) { print $res->content; } else { print "No response\n"; print $res->status_line . "\n"; }

    Now the above code will print out just fine. However, if I try to add something like the following into the above code:

    open(MYFILE, '/tmp/data.txt'); print MYFILE $res->content; close(MYFILE);

    Or if I first assign the response data to a scalar and then do it:

    my $returnData = $res->content; open(MYFILE, '/tmp/data.txt'); print MYFILE $returnData; close(MYFILE);

    The file "/tmp/data.txt" is an empty file (although I didn't check it with "ls -l" which I probably should have). I just did a "cat" on the file and it was blank. I am also pretty sure that the popen command is not affecting things because if I print out a simple string from the same Perl script, that gets captured with popen. It has something to do with this HTTP::Response module.

      Extra tidbits:

      • If something isn't working as expected, one should start by checking what error occured! open, print and close all indicate if they were successful, and if not, why they weren't.

      • Declaring variables before they are used just introduces the possibility of errors. Instead of

        my $x; ... $x = ...;
        do
        ... my $x = ...;
      • Speaking of declaring variables, don't use global variables for file handles. That hasn't been necessary for 10 years.

        open(my $fh, '>', '/tmp/data.txt') or die("Can't create file /tmp/data.txt: $!\n"); print $fh $res->content; close($fh);
      • my $req = HTTP::Request->new(GET =>'http://SOMEURLHERE'); my $res = $ua->request($req);

        can be written as

        my $res = $ua->get('http://SOMEURLHERE');

        Hello All, Thank you so far. I took all your suggestions (e.g. decoded_content(), file handles, variable declarations, etc). It turns out though, that my hunch was right. I should have done an "ls -l" on the file and I would have discovered it was not an empty file. If you hadn't figured out already I am on a Unix/Linux system. Contrary to my original posting I thought I had done a "cat" when in actuality I had done a "more". Funny enough, regardless of whether I decode the content or not, something shows up with the "cat" command but not the "more" command. Also, I am able to edit the file in "vi" and see something. I am still stumped by the output though. Assuming that I know the contents of my file should be two lines of text separated by a newline. For example, "The Cat" and "The Dog" on separate lines:

        If I "cat" the file, I get:

        The Cat
        The Dog

        If I "vi" the file, I get:

        @^T@^h@^e@^ @^C@^a@^t@^
        @^T@^h@^e@^ @^D@^o@^g@^ @^

        Just for giggles, I did an "od -c", and I got:

        0000000 \0 T \0 h \0 e \0 C \0 a \0 t \0 \n \0 T
        0000020 \0 h \0 e \0 D \0 o \0 g \0 \n

        So for the $64 million questions, why is this showing up empty when I "more" the file, why is the calling program seeing it as empty as does the "more" command, and what's with these characters?
        If I decode this with UTF8, should that not take care of it?

      Your open statement

      open(MYFILE, '/tmp/data.txt');
      is opening the file for reading only. (See open.) Try this:
      open(MYFILE, '>', '/tmp/data.txt') || die("Cannot open /tmp/data.txt: +$!");