in reply to Re: Error "HTTP::Message content must be bytes" while posting with LWP
in thread Error "HTTP::Message content must be bytes" while posting with LWP

Yes, i have read this but how i can fix the error?

  • Comment on Re^2: Error "HTTP::Message content must be bytes" while posting with LWP

Replies are listed 'Best First'.
Re^3: Error "HTTP::Message content must be bytes" while posting with LWP
by keszler (Priest) on Nov 11, 2009 at 10:14 UTC
    Two choices. From the above link:
    Seems the latest 'update' to libwww-perl broke things that used to work . . .

    Solution 1: downgrade libwww-perl to an older version.

    . . . this has to do with utf8 stuff in the content . . .

    Solution 2: make sure there's no "utf8 stuff in the content". Looking at your $query I see '...' a couple of times, not as three dots but as a single ellipsis character. Try changing those first; if no joy then keep searching for non-ASCII characters.

      Actually, if you have a string with Unicode content, encode it to utf8 octets, by using Encode::encode:

      use Encode qw(encode); my $body = encode('UTF-8', $content);

      Of course you also need to set up the headers appropriately to indicate to the server that you're sending utf8.

      I had this problem and went through the tedious exercise of understanding unicode, utf-8 etc all over again. Basically the developer of LWP has enforced that strings sent to HTTP::Message - and therefore all the higher functions such as parse to be octet based - as it is in the natural environment of the web. So this means that generating raw html strings in perl and expecting HTTP::Response parsing to work will fail (because Perl uses native utf8 and the module doesn't. This is pretty annoying I know as you assume you shouldn't have to encode perl strings internally - but thats how it is. The easy fix is to force your homemade html strings to be byte based thus:
      use Encode; use HTTP::Response; my $internal_html = my_html_generation_function(); my $enc_html = encode("iso-8859-1",$internal_html); #for the respons +e object to manage in octets; my $response = HTTP::Response->parse($enc_html);