[Catalyst] UTF8 and content length

Kroshka Yenot trashbox at cary.lv
Fri Jul 15 12:45:19 GMT 2016

 >>  The clean way to do this is to simply encode the data before you 
put it in the body:

I fogot or, most likely, didn't realise I need to encode to utf-8 string 
wich is already utf8 in sources. I still need to think over this tricky 
rocket science, but your solution is working.

σας ευχαριστώ

15.07.2016 15:12, Aristotle Pagaltzis пишет:
> * Kroshka Yenot <trashbox at cary.lv> [2016-07-15 13:12]:
>> Hi!
>> if content type is 'application/json' or 'application/json;
>> charset=utf-8' Catalyst sets content length in chars, NOT IN BYTES and
>> I'm getting
>> {"id":1, "msg":"В Питере
>> if content type is 'text/html' Catalyst sets content length in bytes
>> (properly) and everything works fine
> I am guessing you have an encoding configured in Catalyst? If yes, then
> it encodes text/html bodies etc automatically for you, so the body comes
> out in bytes, and its length is then correct, so everything works.
>> Is there any workaround to configure this behaviour, except setting
>> content length manually everytime ?
>> my $json_text = '{"id":1, "msg":"В Питере пить"}';
>> $c->response->content_type('application/json');
>> $c->response->content_length(bytes::length $json_text);
>> $c->response->body($json_text);
>> Thanks in advance
> (Side note: if that code works, you must have `use utf8` in effect.
> Next time you ask about such a problem, please mention this and any
> other relevant parts of your configuration/setup. They are crucial.)
> Here you are using bytes::length, which is broken by design and is
> always the wrong thing to use (unless you are debugging perl itself or
> writing XS code maybe), after putting a character string in the body,
> and then relying on the fact that perl falls back to converting char
> strings to UTF-8 on output because it can’t do anything else.
> This ends up working, but it’s a terrible way to achieve what you need.
> It relies on multiple broken things and workarounds cancelling each
> other in just the right way to get the correct answer. The clean way to
> do this is to simply encode the data before you put it in the body:
>      use utf8;
>      my $json_text = '{"id":1, "msg":"В Питере пить"}';
>      $c->response->content_type('application/json; charset=utf-8');
>      $c->response->body(Encode::encode_utf8 $json_text);
> Regards,

More information about the Catalyst mailing list