By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
438,177 Members | 976 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 438,177 IT Pros & Developers. It's quick & easy.

LWP::UserAgent get just body content(image)

P: 4
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?

Expand|Select|Wrap|Line Numbers
  1. #!/usr/bin/perl
  2.  
  3. use strict;
  4. use Data::Dumper;
  5. use LWP::UserAgent;
  6.  
  7. my $type = "TIFF";
  8. my $ua = new LWP::UserAgent;
  9. my $service = "http://ops.epo.org//soap-services/document-retrieval";
  10.  
  11. my $content = '<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:ops="http://ops.epo.org">';
  12. $content = $content . '<soapenv:Header/>';
  13. $content = $content . '<soapenv:Body>';
  14. $content = $content . "<ops:document-retrieval id=\"EP        1000000A1 I \" page-number=\"1\" document-format=\"SINGLE_PAGE_$type\" system=\"ops.epo.org\">";
  15. $content = $content . '</ops:document-retrieval>';
  16. $content = $content . '</soapenv:Body>';
  17. $content = $content . '</soapenv:Envelope>';
  18.  
  19. my $header = new HTTP::Headers (
  20.         'Content-Type'   => 'text/xml; charset=utf-8',
  21.         'SOAPAction'     => 'document-retrieval',
  22.    );
  23.  
  24. my $req = new HTTP::Request('POST',$service,$header,$content);
  25. my $res = $ua->request($req);
  26. print "request string:\n". $req->as_string."\n";
  27.  
  28. print "content type: ". $res->content_type."\n";
  29. print "header = ". $res->headers_as_string()."\n";
  30.  
  31. #my $response = $res->headers_as_string();
  32. #my $response .= $res->content;
  33. #print "---response---\n$response\n";
  34.  
  35. #my ($body,$mime);
  36. #eval
  37. #{
  38. #   $mime = $res->parts([1]);
  39.    #$body = $mime->body_handle();
  40. #};
  41. #if ($@)
  42. #{ die "error: $@\n"; }
  43.  
  44. if ($res->is_success)
  45. {
  46.   my $dlfile = "file.$type";
  47.   open(OUT, ">$dlfile") or die "whoops $!";
  48.   binmode(OUT);
  49.   print OUT $res->content;
  50.   close OUT;
  51. }
  52. else
  53. { warn "request failed...\n"; }
  54.  
  55.  
Output:

request string:
POST http://ops.epo.org//soap-services/document-retrieval
User-Agent: libwww-perl/5.834
Content-Type: text/xml; charset=utf-8
SOAPAction: document-retrieval

<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:ops="http://ops.epo.org"><soapenv:Header/><soapenv:Body><ops:document-retrieval 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="http://schemas.xmlsoap.org/soap/envelope/"><SOAP-ENV:Header/><SOAP-ENV:Body><ops:world-patent-data xmlns="http://www.epo.org/exchange" xmlns:ops="http://ops.epo.org"><ops:meta 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!
Apr 14 '10 #1
Share this question for a faster answer!
Share on Google+

Post your reply

Sign in to post your reply or Sign up for a free account.