Search code examples
pythonjsonunicoderequestresponse

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.


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