0

I am requiring to send special characters like accented characters with diacritics, e.g. o-acute ó, via API

This is my test code

import string
import http.client
import datetime
import json

def apiSendFarmacia(idatencion,articulo,deviceid):
    ##API PAYLOAD
    now = datetime.datetime.now()
    conn = http.client.HTTPSConnection("apimocha.com")
    payload = json.dumps({
      "idatencion": idatencion,
      "articulo": articulo,
      "timestamp": str(now),
      "deviceId": deviceid
      
    }).encode("utf-8")
    headers = {
      'Content-Type': 'application/json'
    }
    conn.request("POST"
                ,"/xxxx/api2"#"/my/api/path" #"/alexa"
                , payload
                , headers)
    res = conn.getresponse()
    httpcode = res.status
    data = res.read()
    return httpcode#, data.decode("utf-8")
    ##API PAYLOAD

when executing the function with some special characters

apiSendFarmacia(2222,"solución",2222)

the mock API will receive following JSON payload with \u00f3 instead of ó:

{
      "idatencion": 2222,
      "articulo": "soluci\u00f3n",
      "timestamp": "2022-12-07 14:52:24.878976",
      "deviceId": 2222
}

I was expecting the special character with its accent-mark ó to show in the mock API.

hc_dev
  • 8,389
  • 1
  • 26
  • 38
Andres Mora
  • 1,040
  • 8
  • 16
  • And that's exactly what it should do. Doesn't that work? Remember, that string does not actually contain a backslash or a 0. Python just shows it to you like that. – Tim Roberts Dec 07 '22 at 20:07
  • 1
    The resulting JSON is correct. Did you expect anything else? – Klaus D. Dec 07 '22 at 20:08
  • Name the special-characters in your question, e.g. the [o-acute `ó`](https://en.wikipedia.org/wiki/%C3%93) which gets represented as Unicode `\u00f3` in your resulting string. – hc_dev Dec 07 '22 at 20:17

1 Answers1

4

As you print it, it will appear as the special character:

>>> print('soluci\u00f3n')
solución

u00f3 denotes the hexadecimal representation for Unicode Character 'LATIN SMALL LETTER O WITH ACUTE' (U+00F3).

The \ is an escape-signal that tells the interpreter that the subsequent character(s) has to be treated as special character. For example \n is interpreted as newline, \t as tab and \u00f3 as Unicode character ó.

hc_dev
  • 8,389
  • 1
  • 26
  • 38
nokla
  • 451
  • 1
  • 8
  • See also [How to print Unicode character in Python? - Stack Overflow](https://stackoverflow.com/questions/10569438/how-to-print-unicode-character-in-python) or [Unicode HOWTO — Python 3.11.1 documentation](https://docs.python.org/3/howto/unicode.html). – hc_dev Dec 07 '22 at 20:24
  • 1
    Helpful explanation of escaping-concept and Unicode character representation. Hope you don't mind my improving edit. Feel free to add my "See also" links so I can remove my comment. – hc_dev Dec 07 '22 at 20:42