Non Ascii characters are escaped when posting JSON
See original GitHub issueWhen using
import requests
requests.post('https://httpbin.org/post', json={'data': 'My Päload'})
the non asci characters (like ‘ä’ in this case), are getting escaped, resulting in wron data beeing received by the endpoint.
I had to help myself by doing all this stuff manually:
import requests
import json
json_string = json.dumps({'data': 'My Päload'}, ensure_ascii=False)
requests.post('https://httpbin.org/post', data=json_string.encode('utf-8'), headers={'Content-Type': 'application/json'})
Expected Result
Payload should retain special characters:
{"data": "My Päload"}"
Actual Result
Special characters are getting encoded:
{"data": "My P\\u00e4load"}
Reproduction Steps
See summary
System Information
{ "chardet": { "version": "3.0.4" }, "cryptography": { "version": "" }, "idna": { "version": "2.10" }, "implementation": { "name": "CPython", "version": "3.8.0" }, "platform": { "release": "10", "system": "Windows" }, "pyOpenSSL": { "openssl_version": "", "version": null }, "requests": { "version": "2.24.0" }, "system_ssl": { "version": "1010104f" }, "urllib3": { "version": "1.25.9" }, "using_pyopenssl": false }
Issue Analytics
- State:
- Created 3 years ago
- Comments:9 (3 by maintainers)
@hpca01 Your code appears to be pretty specific to your use-case and thus shouldn’t live in the Requests code base.
The way things are now work for most people and if you need something else you can encode yourself as is recommended in this thread.
You can always encode your own JSON and pass it to data= with a content-encoding set.