How to use special characters (accents) on post requests

Viewed 778

I'm new here.

I found this forum through this post, and basically OP is having the same problem as I am, but the answer did not work for me.

What I want is to send a post request that sends a whatsapp message through an API, but if I try to use accents on the letters the message that is sent is completely broken (see the example here) the weird diamond with a question mark was supposed to be an "é" character.

So, my question is: how can I make the "é" be sent through the whatsapp? Am I messing up with encoding or something?

Here's the code of what I'm trying to do:

def send_message():
    headers = {
    'Content-Type': 'application/json;charset=utf-8',
    'x-maytapi-key': API_TOKEN,
    }
    url = INSTANCE_URL + "/" + PRODUCT_ID + "/" + PHONE_ID + "/sendMessage"
    data = '{ "message": "Essa mensagem \xe9 um teste, por favor desconsidere", "to_number": "+55XXXXXXXXXXX", "type": "text" }'
    response = requests.post(url, headers=headers, data=data)

The code above sends the message, but it arrives as seen on that picture (as the weird diamond)

Now here's what I've tried after seeing the forum post mentioned in the beginning:

def send_message():
    headers = {
    'Content-Type': 'application/json',
    'x-maytapi-key': API_TOKEN,
    }
    url = INSTANCE_URL + "/" + PRODUCT_ID + "/" + PHONE_ID + "/sendMessage"
    data = '{ "message": "Essa mensagem \xe9 um teste, por favor desconsidere", "to_number": "+55XXXXXXXXX", "type": "text" }'.encode('latin-1')

    response = requests.post(url, headers=headers, data=data)
    print(response.text)

Result is, unfortunately, the same.

1 Answers

You can try to use json= parameter instead of data= when you do your request. Also, first try to remove the Content-Type header:

def send_message():
    headers = {
        # "Content-Type": "application/json;charset=utf-8",
        "x-maytapi-key": API_TOKEN,
    }
    url = INSTANCE_URL + "/" + PRODUCT_ID + "/" + PHONE_ID + "/sendMessage"
    data = {
        "message": "Essa mensagem é um teste, por favor desconsidere",
        "to_number": "+55XXXXXXXXXXX",
        "type": "text",
    }

    response = requests.post(url, headers=headers, json=data)
Related