mifflin has asked for the wisdom of the Perl Monks concerning the following question:

Have have a small program using LWP to do a Post. The response is XML. Here is my program...
use LWP::UserAgent; use HTTP::Request::Common qw(POST); my $ua = LWP::UserAgent->new() || die "Unable to create new UserAgent +object"; my $url = 'http://cosmias02d.mwh.com:7778/reports/rwservlet'; my %form_data = ( SERVER => 'repcosmias02d', USERID => 'mwh/prahd@dev', REPORT => 'ap_daily_jrnl.rep', DESTYPE => 'PRINTER', DESNAME => 'cohpisd012', DESFORMAT => 'hplwide', STATUSFORMAT => 'xml' ); my $request = POST($url, \%form_data); my $response = $ua->request($request); $response->remove_header(); $content = $response->as_string(); print "$content\n";
The response is
HTTP/1.1 200 OK
Cache-Control: private
Connection: close
Date: Tue, 09 Sep 2003 23:08:26 GMT
Server: Oracle9iAS/9.0.2 Oracle HTTP Server
Content-Language: en
Content-Type: text/xml
Client-Date: Tue, 09 Sep 2003 23:08:27 GMT
Client-Peer: 172.25.209.185:7778

<?xml version = '1.0' encoding = 'ISO-8859-1' standalone = 'yes'?>
<serverQueues>
   <job id="117" queueType="past">
      <name>ap_daily_jrnl.rep</name>
      <type>report</type>
      <status code="4">Finished successfully</status>
      <owner>RWUser</owner>
      <server>repcosmias02d</server>
      <destination>
         <desType>PRINTER</desType>
         <desName>cohpisd012</desName>
         <desFormat>unknown</desFormat>
         <file>39872723.txt</file>
      </destination>
      <timingInfo>
         <queued>9/9/03 4:08 PM</queued>
         <started>9/9/03 4:08 PM</started>
         <finished>9/9/03 4:08 PM</finished>
      </timingInfo>
   </job>
</serverQueues>
What is the best way to strip off the header section so I can pass the xml document into an xml parser?

edited: Wed Sep 10 02:41:28 2003 by jeffa - code tags for code (left the pre tags for response)

Replies are listed 'Best First'.
Re: What is the best way to get the response payload
by DaveH (Monk) on Sep 09, 2003 at 23:34 UTC

    Hi.

    Can you use:

    $response->content();

    Rather than:

    $response->as_string();

    Note: this is untested.

    Cheers,

    -- Dave :-)


    $q=[split+qr,,,q,~swmi,.$,],+s.$.Em~w^,,.,s,.,$&&$$q[pos],eg,print
Re: What is the best way to get the response payload
by tachyon (Chancellor) on Sep 10, 2003 at 04:15 UTC
    $xml = $response->code == 200 ? $response->content : 'Error'

    cheers

    tachyon

    s&&rsenoyhcatreve&&&s&n.+t&"$'$`$\"$\&"&ee&&y&srve&&d&&print

Re: What is the best way to get the response payload
by eric256 (Parson) on Sep 09, 2003 at 23:44 UTC

    Arn't HTTP headers terminated with a double new line?

     my ($header,$content) = split(/\n\n/,$response->as_string());

    I'm no expert with split but that should be sufficient :)

    ___________
    Eric Hodges

      Of course, I'm sure you intended:

      my($header, $content) = split(/\n\n/, $response->as_string, 2);

      (Otherwise, if the body contained any blank lines, the blank lines would be lost...)

      I prefer the solution posted by somebody else: $response->content.

Re: What is the best way to get the response payload
by BUU (Prior) on Sep 09, 2003 at 23:31 UTC
    Assuming your reading from a file handle or something..
    while(<RESPONSE>){last if /^<?xml/} while(<RESPONSE>) { #all the lines read here will be the xml document. }