Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

LWP::UserAgent get just body content(image)

Status
Not open for further replies.

gcw2001

Programmer
Apr 8, 2010
5
0
0
US
Hi all,
I am attempting to use the LWP::UserAgent to request a Soap call, and in the response the request returns a TIFF image. When I write the contents out to a file(binary mode), the file also contains header information. How would I go about extracting just the body(TIF image) of the response?

Code:
#!/usr/bin/perl

use strict;
use Data::Dumper;
use LWP::UserAgent;

my $type = "TIFF";
my $ua = new LWP::UserAgent;
my $service = "[URL unfurl="true"]http://ops.epo.org//soap-services/document-retrieval";[/URL]

my $content = '<soapenv:Envelope xmlns:soapenv="[URL unfurl="true"]http://schemas.xmlsoap.org/soap/envelope/"[/URL] xmlns:ops="[URL unfurl="true"]http://ops.epo.org">';[/URL]
$content = $content . '<soapenv:Header/>';
$content = $content . '<soapenv:Body>';
$content = $content . "<ops:document-retrieval id=\"EP        1000000A1 I \" page-number=\"1\" document-format=\"SINGLE_PAGE_$type\" system=\"ops.epo.org\">";
$content = $content . '</ops:document-retrieval>';
$content = $content . '</soapenv:Body>';
$content = $content . '</soapenv:Envelope>';

my $header = new HTTP::Headers (
        'Content-Type'   => 'text/xml; charset=utf-8',
        'SOAPAction'     => 'document-retrieval',
   );

my $req = new HTTP::Request('POST',$service,$header,$content);
my $res = $ua->request($req);
print "request string:\n". $req->as_string."\n";

print "content type: ". $res->content_type."\n";
print "header = ". $res->headers_as_string()."\n";

#my $response = $res->headers_as_string();
#my $response .= $res->content;
#print "---response---\n$response\n";

#my ($body,$mime);
#eval
#{
#   $mime = $res->parts([1]);
   #$body = $mime->body_handle();
#};
#if ($@)
#{ die "error: $@\n"; }

if ($res->is_success)
{
  my $dlfile = "file.$type";
  open(OUT, ">$dlfile") or die "whoops $!";
  binmode(OUT);
  print OUT $res->content;
  close OUT;
}
else
{ warn "request failed...\n"; }

Output:

request string:
POST User-Agent: libContent-Type: text/xml; charset=utf-8
SOAPAction: document-retrieval

<soapenv:Envelope xmlns:soapenv=" xmlns:eek:ps=" id="EP 1000000A1 I " page-number="1" document-format="SINGLE_PAGE_TIFF" system="ops.epo.org"></ops:document-retrieval></soapenv:Body></soapenv:Envelope>

content type: multipart/related
header = Connection: close
Date: Wed, 14 Apr 2010 13:31:58 GMT
Accept: text/xml, text/html, image/gif, image/jpeg, *; q=.2, */*; q=.2
Server: Apache
Content-Type: multipart/related; boundary="----=_Part_901910_514465450.1271251918535"; type="text/xml"
Client-Date: Wed, 14 Apr 2010 13:32:00 GMT
Client-Peer: 145.64.132.201:80
Client-Response-Num: 1
Client-Transfer-Encoding: chunked
SOAPAction: ""
X-Powered-By: Servlet 2.4; JBoss-4.3.0.GA (build: SVNTag=JBPAPP_4_3_0_GA date=200801031548)/Tomcat-5.5

Top few lines of TIFF file:
------=_Part_901910_514465450.1271251918535
Content-Type: text/xml; charset=utf-8

<SOAP-ENV:Envelope xmlns:SOAP-ENV=" xmlns=" xmlns:eek:ps=" name="elapsed-time" value="42"/><ops:document-retrieval document-format="SINGLE_PAGE_TIFF" id="EP 1000000A1 I " page-number="1" system="ops.epo.org"><ops:desc>FullDocument</ops:desc><ops:content-ref>EP 1000000A1 I .tiff</ops:content-ref></ops:document-retrieval></ops:world-patent-data></SOAP-ENV:Body></SOAP-ENV:Envelope>
------=_Part_901910_514465450.1271251918535
Content-Type: application/tiff
Content-ID: EP 1000000A1 I .tiff
Content-Transfer-Encoding: binary


Thanks for any replies!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top