question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Non Ascii characters are escaped when posting JSON

See original GitHub issue

When 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:closed
  • Created 3 years ago
  • Comments:9 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
sethmlarsoncommented, Nov 9, 2020

@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.

1reaction
sethmlarsoncommented, Sep 4, 2020

You can always encode your own JSON and pass it to data= with a content-encoding set.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Why does json.dumps escape non-ascii characters with "\uxxxx"
In Python 2, the function json.dumps() will ensure that all non-ascii characters are escaped as \uxxxx . Python 2 Json. But isn't this...
Read more >
Python JSON Encode Unicode and non-Ascii characters as-is
Let' see how store all incoming non-ASCII characters escaped in JSON. It is a safe way of representing Unicode characters. By setting ...
Read more >
JSON escape non-ascii characters | Apple Developer Forums
Hi,. I´m using Newtonsoft JSON on my c# application and it has the option to escape non-ascii characters when serializing an object.
Read more >
Spring Boot: escape HTML and non-ASCII characters in Json ...
Spring Boot: escape HTML and non-ASCII characters in Json response. Create an ObjectMapper object and let Spring Boot use our ObjectMapper ...
Read more >
Problems with HTML escaping and non-ASCII characters ...
Problems with HTML escaping and non-ASCII characters using the JSON OSLC API ... Fabian Zaiser (23○2○6) | asked Aug 09 '16, 10:18 a.m....
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found