I am trying to use LWP::UserAgent to get data from a simple web server I have created on an Arduino.

The request succeeds, but the decoded_content contains all of the response header. The information contained in the headers_as_string value is not sent from the server. The server sends "\r\n" for all line endings and an blank line with "\r\n" to mark the end of the header.

I have tried adding various attributes to the response header, but all had the same result of the header being in the decoded_content value.

Any thoughts on what may be causing the headers to be included in the content?

Thanks

Perl code
#!/opt/local/bin/perl use LWP::UserAgent; use JSON; use warnings; use strict; my $ua = LWP::UserAgent->new; my $server_endpoint = "http://192.168.0.198/LED1"; my $req = HTTP::Request->new( GET => $server_endpoint ); $req->header('content-type' => 'application/json'); my $resp = $ua->request($req); if ( $resp->is_success ) { my $header = $resp->headers_as_string; print "Received header:\n$header\n---------\n"; my $message = $resp->decoded_content; print "Received reply:\n$message\n---------\n"; } else { print "HTTP GET error code: ", $resp->code, "\n"; print "HTTP GET error message: ", $resp->message, "\n"; }
Output from above code
Received header: Client-Date: Mon, 17 Mar 2014 22:45:08 GMT Client-Peer: 192.168.0.198:80 Client-Response-Num: 1 --------- Received reply: HTTP/1.1 200 OK Content-Type: application/json Connection: close {"val":1} ---------

In reply to LWP UserAgent response header in decoded_content by jeff_e

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.