Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to get not encoded characters in response of http post in Python

I’m calling GraphQL API via http post, as:

result = json.dumps(requests.post(url, json={'query': query}).json(), indent=2)

In result I see some characters are converted to their Unicode codes, like: \u00c2\u00ae\u00c2 but I wish to get actual characters there, like: ®Â.
When I’m doing the same with curl command, I’m getting these characters.
I’ve tried to use .text property. Tried .decode('utf8') but still getting codes not actual character.

Please advise.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

The json.dumps() function has an option "ensure_ascii" which is "True" on default. This way all characters in the json string will be ascii characters. Just add ensure_ascii=False to your json.dumps() call and it should do what you want.
https://docs.python.org/3/library/json.html#json.dump

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading