PROBLEM
I am using the Brazilian version of the Mercado Libre API to publish products. Whenever I post a JSON, the accents are being removed by their system and this, I expect, should not at all be the default behavior because the Portuguese language has all of these accents. Their API documentation does not mention anything about this.
WHAT I HAVE TRIED
I tried to encode and decode it multiple times in various ways and using UTF-8 and latin1 with and without json.dumps().
My last attempt was adding:
headers['Content-Type'] = "application/json;charset=UTF-8"
All this did was, instead of vanishing completely, the accented letters were replaced by escape sequences such as:
Curto a Médio -> Curto a M\xe9dio
I also tried without the "charset=UTF-8" part and the accents were still escaped.
CODE
headers = {
'Authorization': f"Bearer {ACCESS_TOKEN}",
}
if request_type == 'Product upload':
headers['Content-Type'] = "application/json;"
response = requests.post(url, headers=headers, data=data)
DATA
[
{
"id":"GENDER",
"value_name":"Mulher"
},
{
"id":"BRAND",
"value_name":"Torricella"
},
{
"id":"MODEL",
"value_name":"65019A"
},
{
"id":"FOOTWEAR_TYPE",
"value_name":"Botas"
},
{
"id":"FOOTWEAR_STYLE",
"value_name":"Bota Bico Fino"
},
{
"id":"SHAFT_TYPE",
"value_name":"Curto a Médio"
},
{
"id":"RELEASE_SEASON",
"value_name":"Primavera/Verão"
},
{
"id":"RELEASE_YEAR",
"value_name":2021
}
]
Unfortunately they do not provide support of any kind to people who are not official partners.
What else could I try? I think it's unlikely that this is a problem with their API since they are worth 4 freaking billion dollars and their API has been around for ages.
Using json.dumps(data)
without ensure_ascii=False
worked!
Indeed, if ensure_ascii=True
(the default), the output is guaranteed to have all incoming non-ASCII characters correctly escaped.