LWP::UserAgent get just body content(image)

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • gcwg
    New Member
    • Apr 2010
    • 4

    LWP::UserAgent get just body content(image)

    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 = "http://ops.epo.org//soap-services/document-retrieval";
    
    my $content = '<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:ops="http://ops.epo.org">';
    $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 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:Envelo pe xmlns:soapenv=" http://schemas.xmlsoap .org/soap/envelope/" xmlns:ops="http ://ops.epo.org"><s oapenv: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:Envelop e>

    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_5 14465450.127125 1918535"; 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=2008010315 48)/Tomcat-5.5

    Top few lines of TIFF file:

    ------=_Part_901910_5 14465450.127125 1918535
    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:w orld-patent-data xmlns="http://www.epo.org/exchange" xmlns:ops="http ://ops.epo.org"><o ps:meta name="elapsed-time" value="42"/><ops:documen t-retrieval document-format="SINGLE_ PAGE_TIFF" id="EP 1000000A1 I " page-number="1" system="ops.epo .org"><ops:desc >FullDocument </ops:desc><ops:c ontent-ref>EP 1000000A1 I .tiff</ops:content-ref></ops:document-retrieval></ops:world-patent-data></SOAP-ENV:Body></SOAP-ENV:Envelope>
    ------=_Part_901910_5 14465450.127125 1918535
    Content-Type: application/tiff
    Content-ID: EP 1000000A1 I .tiff
    Content-Transfer-Encoding: binary


    Thanks for any replies!
Working...