1

I have some json containing some thai values. It looks something like

{
   "TitleName": "คุณ",
   "FirstName": "Simar"
}

I need to make a Http POST request with this json body with the exact thai value.I am using Python 3 requests library to make the request. I tried this

headers = {
        'Content-Type': "application/json",
        'Authorization': "xxx",
        'cache-control': "no-cache",
        'Postman-Token': "xxx"
    }    
response = requests.request("POST", url, json=request, headers=headers)

It generates json values as

"TitleName": "\\u0e04\\u0e38\\u0e13",
"FirstName": "Simar"

I also tried this

json_request = json.dumps(self.request_data,ensure_ascii=False).encode('utf8')     
response = requests.request("POST", url, json=json_request, headers=headers)

It generates json values as

"TitleName": "\xe0\xb8\x84\xe0\xb8\xb8\xe0\xb8\x93",
"FirstName": "Simar"

But I want json values to be generated as

   "TitleName": "คุณ",
   "FirstName": "Simar"

Help will be appreciated. Thanks in advance.

S.K
  • 480
  • 1
  • 4
  • 19
  • Why do you need to keep it as is? I think Python does not print weird characters (like emojis), that does not mean you cannot use it on a website. – Matt Bussing Sep 06 '19 at 17:03
  • In JSON, \u0e04 is the same character as คุ. (Is \\u0e04 just a Python literal representation of a text string that happens to contain JSON, perhaps as shown by a debugger?) – Tom Blodget Sep 07 '19 at 13:07
  • I want to keep it as it is, because the API which I want to hit works only with the thai value, and not with the literals like "\u0e04" or "\xe0" – S.K Sep 08 '19 at 07:07
  • @TomBlodget yes I picked this value from the debugger – S.K Sep 08 '19 at 07:08
  • Thanks for explaining. \x notation is not valid JSON but \u is. Too bad your client is non-compliant. I don't have a better solution than @snakecharmerb (but others might). – Tom Blodget Sep 08 '19 at 14:43

1 Answers1

0

To preserve non-ascii characters in POST requests you need to serialise to json manually, and explicitly set the content-type header.

data = json.dumps(my_dict, ensure_ascii=False)
r = requests.post(url, headers={'content-type': 'application/json'},
                  data=data.encode('utf-8'))
snakecharmerb
  • 47,570
  • 11
  • 100
  • 153
  • I think I did the same thing already as mentioned in the question. But it gives me the value as "TitleName": "\xe0\xb8\x84\xe0\xb8\xb8\xe0\xb8\x93" – S.K Sep 08 '19 at 07:04
  • Can you try setting the header as `'application/json;charset=utf8'`? You have got similar code in your question, but requests requires that you set the content-type header when doing this, and you question doesn't show which headers you're sending. – snakecharmerb Sep 08 '19 at 08:28
  • I already tried with header as `'application/json'` and also with `'application/json; charset=utf8'`. But none of it worked. – S.K Sep 08 '19 at 12:03
  • Also, I have edited the question to specify the headers used. – S.K Sep 08 '19 at 12:05