Description of the problem
When there is Chinese in the response of the interface implemented by flask, the interface returns unicode garbage.
This is actually ASCII.
Use an online conversion tool to convert and see the results.
problem screening
A lot of information found online says to turn off the ascii encoding method when creating a flask app
app = Flask(__name__) ['JSON_AS_ASCII'] = False # Solve Chinese messy code problem
this one is forflask 2.2.5The following version does work, but the higher version fails this wizard spirit.
Luckily I have multiple versions of my development environment Flask;
Slightly older: Flask 2.2.5
Latest version: Flask 2.3.3
If two versions of the same code work and one doesn't, then it's basically the version.
So I went to the official version of the changelog, and finally found the original version 2.3.0 has abandoned the JSON_AS_ASCII property. So how to deal with the new version, here will not repeat, first directly on themethod settle an issue, for those who want to delve deeper, you can flip to the back and continue reading.
Problem solving
flask 2.3.0that amount or more
app = Flask(__name__) .ensure_ascii = False # Solve Chinese messy code problem
flask 2.2.5that amount or less
app = Flask(__name__) ['JSON_AS_ASCII'] = False # Solve Chinese messy code problem
Restart the project code.
The reason why we need to turn off ascii encoding is because it is enabled by default when Flask initializes the app, so we can turn it on or off as needed when we work on our own projects.
At this point, look at the interface return, has been normal return Chinese.
Pitfalls in Flask Versions
flask update log:
Changes — Flask Documentation (2.)
As you can see from the image above, the JSON_AS_ASCII attribute has been removed in version 2.3.0.
So how do you set up the new version, as this article below has pointed out.
API — Flask Documentation (2.)
For those who don't speak English well, you can read the translation
The above is a new version of Flask implementation of the interface response in the presence of Chinese interface return to unicode messy solution in detail, more about the new version of Flask return to unicode messy solution of Chinese information please pay attention to my other related articles!