Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

incorrect length in JSON? #143

Open
jjn1056 opened this issue Jul 19, 2016 · 5 comments
Open

incorrect length in JSON? #143

jjn1056 opened this issue Jul 19, 2016 · 5 comments

Comments

@jjn1056
Copy link
Member

jjn1056 commented Jul 19, 2016

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
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

@jjn1056
Copy link
Member Author

jjn1056 commented Jul 19, 2016

Looks like to a bug to me, although I'm not personally keen on the auto length setting in Catalyst it should be corrected. I'm happy to get a patch, or at the very least give me a broken test case (checkout https://github.com/perl-catalyst/catalyst-runtime/blob/master/t/utf_incoming.t

and see if you can help me figure it out -jnap

@melmothx
Copy link
Contributor

If you ask me, this is not a bug. application/json is not encoded by catalyst (as per doc) so it must be encoded to bytes before setting the body. Most serializers do that, so the wrong behavior here is just because of the manual json creation.

@jjn1056
Copy link
Member Author

jjn1056 commented Jul 24, 2016

@melmothx good catch, you'd think I'd remember that since I wrote the doc and made that call for back compat reasons. I wonder if the docs could not be more obvious and maybe I should add a config setting to 'force_json_encode_always' for people that are rolling their own... I guess I was thinking if you are doing that yourself I assume you know what you are doing...

@jjn1056
Copy link
Member Author

jjn1056 commented Jul 24, 2016

I'm thinking the docs should at least recommend one considers setting content length in your custom view, just to be safe.

@melmothx
Copy link
Contributor

John Napiorkowski notifications@github.com writes:

I'm thinking the docs should at least recommend one considers setting
content length in your custom view, just to be safe.

I believe that if the string is passed correctly, the content length is
set by catalyst correct as well (or at least works for me so). In the OP
report, the length was off because chars were passed and setting the
length was not going to make the situation better (because of broken
encoding).

Marco

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants