473,387 Members | 1,365 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

LWP::UserAgent get just body content(image)

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
0 1851

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

Similar topics

1
by: Pawel Banys | last post by:
Hello, There is a form which contains the following element: <FORM name="testForm" method="post" action="http://server/some_script"> ......../ some standard entry fields /....... <input...
7
by: JavaScriptRocks | last post by:
I've been trying to imitate / reverse engineer the add attachment feature in gmail composer. I managed to do it to say about 80% but its giving me trouble in IE on WinXP-Sp2. I am using PHP to do...
0
by: sharif | last post by:
Anyone could help me out for n=my code ......I have written following code ,Here i m able to get and post the form successfuly..but after posting im not gettng proper response content... ...
0
by: sachintandon | last post by:
Hello all, Thanks in advance for your help I have a problem in sending emails, my requirement is to send multipart alternative emails with attachments, I'm able to send text with attachments or...
9
by: perls | last post by:
"Newbie needs help" Hi all, I had a programmer do a site scraping script for me.. the aim was to scrape data from 5 different sites and upload directly into my website databse. I started to...
4
by: kkamath | last post by:
Hi, I use LWP::UserAgent to automate windows login process. I set the credentials using $ua->credentials("host:port","realm","id","password"); but it fetches only the current page requested. It...
1
by: andrewwan1980 | last post by:
I need help in parsing unicode webpages & downloading jpeg image files via Perl scripts. I read http://www.cs.utk.edu/cs594ipm/perl/crawltut.html about using LWP or HTTP or get($url) functions &...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.