Skip to content

response.json() is failing enconding for non ascii characters #4702

@maufonfa

Description

@maufonfa

Summary.
I'm executing a simple GET request with a list of data. But the .json() fails because there's a non ascii character and it can't encode it.

Expected Result

[
{
"key": "value"
}
]

Actual Result

*** UnicodeEncodeError: 'ascii' codec can't encode character '\xfc' in position 329: ordinal not in range(128)

Reproduction Steps

Try to pass this type of character (In this case is the "ü") in a request and then try to convert the response to json using response.json()

import requests

response = requests.request("GET", ulr, headers=headers)
response.json()

System Information

$ python -m requests.help
{
  "chardet": {
    "version": "3.0.4"
  },
  "cryptography": {
    "version": ""
  },
  "idna": {
    "version": "2.6"
  },
  "implementation": {
    "name": "CPython",
    "version": "3.5.2"
  },
  "platform": {
    "release": "3.10.0-327.13.1.el7.x86_64",
    "system": "Linux"
  },
  "pyOpenSSL": {
    "openssl_version": "",
    "version": null
  },
  "requests": {
    "version": "2.19.1"
  },
  "system_ssl": {
    "version": "1000114f"
  },
  "urllib3": {
    "version": "1.22"
  },
  "using_pyopenssl": false
}

This command is only available on Requests v2.16.4 and greater. Otherwise,
please provide some basic information about your system (Python version,
operating system, &c).

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions