* Kroshka Yenot <trash...@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, -- Aristotle Pagaltzis // <http://plasmasturm.org/> _______________________________________________ List: Catalyst@lists.scsys.co.uk Listinfo: http://lists.scsys.co.uk/cgi-bin/mailman/listinfo/catalyst Searchable archive: http://www.mail-archive.com/catalyst@lists.scsys.co.uk/ Dev site: http://dev.catalyst.perl.org/