faust icon indicating copy to clipboard operation
faust copied to clipboard

append Content-Type: application/json; charset=utf-8

Open hlacikd opened this issue 2 years ago • 3 comments

When using @app.page and returning value of faust.Records object as self.json(object_name)

response is returned with Content-Type: application/json instead of Content-Type: application/json; charset=utf-8

even tho faust claims that it uses utf-8 encoding during json serialization.

This causes major issue for clients that are not de-serializing (decoding) "utf-8" by default --- for example very well known Flutter from google (it is deserialized as ASCII)

Unfortunatelly currently i have found no way to append it to content-type header other than workarounding it as sending response as text and using manual serialization using json library.

        return self.text(
            json.dumps(records, default=str, ensure_ascii=False),
            headers={"Content-Type": "application/json"},
        )

which will report as

< HTTP/1.1 200 OK
< Content-Length: 736
< Content-Type: application/json; charset=utf-8
< Date: Mon, 23 Oct 2023 11:21:24 GMT
< Server: Python/3.10 aiohttp/3.8.6

hlacikd avatar Oct 23 '23 11:10 hlacikd

could you maybe provide a minimal example because with the following


class Hello(faust.Record):
    name: str


@app.page("/hello/")
async def hello(web, request):
    return web.json(Hello(name="world"))

I do get:

response: {"name":"world","__faust":{"ns":"app.Hello"}} headers:

HTTP/1.1 200 OK
Content-Type: application/json; charset=utf-8
Content-Length: 45
Date: Wed, 25 Oct 2023 20:04:33 GMT
Server: Python/3.9 aiohttp/3.8.4

which seems what you want. (tested with chrome)

EDIT: also it is running on macOS 14.0 to produce this.

dada-engineer avatar Oct 25 '23 20:10 dada-engineer

I do get:

response: {"name":"world","__faust":{"ns":"app.Hello"}} headers:

HTTP/1.1 200 OK
Content-Type: application/json; charset=utf-8
Content-Length: 45
Date: Wed, 25 Oct 2023 20:04:33 GMT
Server: Python/3.9 aiohttp/3.8.4

Hello, i am getting following headers on chrome

HTTP/1.1 200 OK
Content-Length: 642
Content-Type: application/json
Date: Fri, 27 Oct 2023 10:51:21 GMT
Server: Python/3.10 aiohttp/3.8.6

so same as curl (no charset=utf-8)

I am on Ubuntu 23.10 ,

could it be somehow related to locales (i am running it in docker container)

I have tried to set LANG=en_US.UTF-8, but no change ...

I believe it would be related to aiohttp behavior, as faust streaming is using it

hlacikd avatar Oct 27 '23 10:10 hlacikd

Hmm when I run the application within a docker debian python 3.11-slim image and request from another docker running ubuntu I still get the expected header.

Connected to host.docker.internal (192.168.65.254) port 6066 (#0)
> GET /hello/ HTTP/1.1
> Host: host.docker.internal:6066
> User-Agent: curl/7.81.0
> Accept: */*
>
* Mark bundle as not supporting multiuse
< HTTP/1.1 200 OK
< Content-Type: application/json; charset=utf-8
< Content-Length: 45
< Date: Fri, 27 Oct 2023 18:26:23 GMT
< Server: Python/3.11 aiohttp/3.8.6

Could zoy provide an example to reproduce this?

dada-engineer avatar Oct 27 '23 18:10 dada-engineer