How are you processing $content?

I tried a modifed version of your code (see below) and I was able to view 
the graphic.

If you are getting \012's stripped out, it sounds like Perl is treating 
$content as a text file not as a binary. Depending on your OS, Perl will 
strip out LF (\012), CR (\013), or do nothing when working with what it 
considers text.

Here's the code I tested. Note that I set the filehandle to binary (with 
binmode) before writing the contents to disk.

use LWP::UserAgent;

my ($host, $doc)=@ARGV;

my $ua=new LWP::UserAgent;
my $req = new HTTP::Request 'GET', "http://$host$doc";;


my $response = $ua->request($req);  # returns a hashref

my $hdrs = $response->headers->as_string();
my $result = $response->status_line;
my $content = $response->content();

print ($hdrs, $result);

open (FILE, ">temp.gif") or die "Could not open output!: $!";

binmode (FILE); # needed for win32

print FILE $content;

close FILE;


On Thu, 25 Apr 2002, Vicki Brown wrote:

> Date: Thu, 25 Apr 2002 22:37:03 -0700
> From: Vicki Brown <[EMAIL PROTECTED]>
> To: "'[EMAIL PROTECTED]'" <[EMAIL PROTECTED]>
> Subject: binary responses via LWP
> 
> My send/receive code is working fine (apparently) for HTML and also for text
> (although oddly, text comes back from the remote CGI with a Content-type of
> text/x-usenet-faq which neither I nor my browser had heard of, so it was
> being downloaded instead of displayed; took me ages to figure out).
> 
> Anyway, my problem is when the response is binary, e.g. a compressed archive,
> a JPEG, a GIF.
> 
> As near as I can determine, I am losing a bunch of ^L (ASCII \012) in the
> response content.  Is there something special I need to do to say "it's
> binary, it's not a string, leave it alone"?
> 
> 
>     my ($host, $port, $doc)=@_;
> 
>     my $ua=new LWP::UserAgent;
>     my $req = new HTTP::Request 'GET', "http://$host$doc";;
> 
> 
>     my $response = $ua->request($req);  # returns a hashref
> 
>     my $hdrs = $response->headers->as_string();
>     my $result = $response->status_line;
>     my $content = $response->content();
> 
>     return($content, $result, $hdrs);
> 
> On the other side, if I run the returned $content through a filter
>     $string =~ s/([^a-zA-Z0-9_])/sprintf("\\%03o", ord($1))/eg;
> and then compare the result with the original file (before I ut it on the
> server), the "new" version I got back through LWP is missing a bunch of \012.
> 
> If I bypass the LWP gateway and go directly to the remote CGI, the files are
> not mangled.
> 
> Is this another simple thing I can't seem to find in the docs?
> -- 
> - Vicki
> 
> Vicki Brown     ZZZ                  Journeyman Sourceror:
> P.O. Box 1269      zz  |\     _,,,---,,_        Scripts & Philtres
> San Bruno, CA       zz /,`.-'`'    -.  ;-;;,_     Perl, Unix, MacOS
> 94066     USA         |,4-  ) )-,_. ,\ ( `'-'
> mailto:[EMAIL PROTECTED]  '---''(_/--'  `-'\_)  http://www.cfcl.com/~vlb
> 

Reply via email to