1

The following code gives an error:

headers = {'Path': '00ab536d1c45d0e21f2726c70ca78a21_كلمة سمو.docx',
           'IssueNo': '', 'IssueDate': '2020-01-27', 'IssueYear': '',
           'CountryName': '', 'DocSource': '66',
           'FileName': '00ab536d1c45d0e21f2726c70ca78a21_كلمة  الأردن.docx',
           'DocNumber': '', 'CreateDMSDoc': '1',
           'apiKey': '4B30A7BB-05E8-4F7D-A195-093CFA578995'}

response = requests.post('http://localhost/FileUploadService/Api/UploadtStream', files=file, headers=headers)

The error is:

UnicodeEncodeError: 'latin-1' codec can't encode characters in position 33-36: ordinal not in range(256)

How do I fix this?

Mark Tolonen
  • 166,664
  • 26
  • 169
  • 251
saju
  • 11
  • 2
  • 1
    Does this answer your question? [What character encoding should I use for a HTTP header?](https://stackoverflow.com/questions/4400678/what-character-encoding-should-i-use-for-a-http-header) – Klaus D. Jan 27 '20 at 15:36

2 Answers2

1

you need to set the character set to be utf-8, or the appropriate Arabic charset. Latin cannot encode for Arabic characters :

headers={'Path': '00ab536d1c45d0e21f2726c70ca78a21_كلمة سمو الوزير منتدى المستقبل الأردن.docx'.encode('utf-8'),
'IssueNo': '', 'IssueDate': '2020-01-27', 'IssueYear': '', 'CountryName': '',
'DocSource': '66',
'FileName': '00ab536d1c45d0e21f2726c70ca78a21_كلمة سمو الوزير منتدى المستقبل الأردن.docx'.encode('utf-8'),
'DocNumber': '', 'CreateDMSDoc': '1',
'apiKey': '4B30A7BB-05E8-4F7D-A195-093CFA578995'}

You need to specificly encode each arabic string to utf-8.

zsuperben
  • 56
  • 4
-1

This is a problem with the arabic characters that cannot be encoded in latin-1, you could try encoding with utf-16 or utf-8 with this comment at the beginning of the python script: # -- coding: utf-8 --

thesylio
  • 144
  • 7
  • 1
    The `# coding: utf-8` comment is just for string literals in the source code. And it's UTF-8 by default already. I don't think this would help here. – lenz Jan 27 '20 at 22:56
  • You're right for python3, but I don't know which version is used here and this link (https://docs.python.org/2/howto/unicode.html) says that Python’s(2) default encoding is the ‘ascii’ encoding. Still I agree it's probably not a solution... – thesylio Jan 28 '20 at 12:08
  • You are right about Python 2, but when Py2 (finally!) reached its end of life last year, I started assuming Python 3, unless it is stated otherwise or there is a clear hint (like a print statement without parens). – lenz Jan 28 '20 at 13:21