0

I'm triying to figure out why this basic Flask view function:

@production.route('/prod/article')
def test_json():
    description = u'HAMPDÅN'
    article = {'id': 0, 'description':  description }
    print(article)
    return article

Is returning this:

{
  "description": "HAMPD\u00c5N", 
  "id": "0"
}

But reading Python console print(article) is returning this:

{'id': '0', 'description': 'HAMPDÅN'}

What am I doing wrong? Its Flask problem when handling the HTTP Response? Because as far as I know, if the print its outputting OK the "HAMPDÅN" its not a problem about encoding, at least from my code part.

So don't know whats happening here... Thanks for any help!

1 Answer 1

2

First of all, you're doing everything correctly.

The "malformed output" is caused by an attempt to represent the raw data of a HTTP message body in a human-friendly way. In reality, the raw data is just a bunch of bytes, which could be represented as a bunch of 0s and 1s, or using hexadecimal notation, but because the body of a HTTP message usually contains text, the raw data of a HTTP message is typically represented as text.

For example, the bytes '0x48 0x45 0x4C 0x4C 0x4F' will be represented as 'HELLO'. However, when the message body contains Unicode characters, they will be represented using the notation with a \u prefix. In this case, the raw bytes of the Unicode character Å are '0xc3 0x85', but are represented using \u00c5.

When your console prints the dictionary, it is not trying to represent the dictionary as the raw data of the HTTP message body, but instead, it will correctly display the bytes '0xc3 0x85' as a proper unicode character. Similarly, whichever client (e.g. browser) receives a HTTP response from you is responsible for correctly decoding the raw bytes of the HTTP message body as UTF-8 and display the characters correctly.

Sign up to request clarification or add additional context in comments.

1 Comment

OMG. So I was doing it correctly... It was just the browser representing the response as just RAW data... I've been the whole day trying to figure out where was the mistake. I now see that I maybe should be using some real client for testing the final data.Thanks a lot!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.