3

I need to post a file using request module in python, which has unicode characters in filename.

I am using below code:

url = "https://demo.php"
headers = {'Accept': 'application/vnd.ve.v1.0+json','API': 'aasadadas'}
file_up = {'filename': open(file_name, 'rb')}
upload_file_rest =requests.post(url,files=file_up,headers=headers,verify=False)

Using the above code and when passing the filename as "指事字.exe", I am getting below exception:

'ascii' codec can't decode byte 0xc2 in position 26: ordinal not in range(128)

Any help is really appreciated.

PS: I had already tried below code, and it doesn't worked for me:

file_up = {'filename': open(file_name.encode('utf-8'), 'rb')}
Apoorv Gupta
  • 399
  • 1
  • 4
  • 16

2 Answers2

0

Alas, you didn't post a stack trace or which version of Python you're using, so here is some guessword involved. My first assumption is that the exception comes from the line which tries to open the file, not from within the requests module.

Make sure you declare the encoding of your python file in the first line:

# -*- coding: utf-8 -*-

Use unicode constants, or make sure your strings are unicode:

filename = u"指事字.txt"

After that, it should be possible to open the file. This snippet works on my computer (Macbook, Python 2.7.10):

filename = u"指事字.txt"
f = open(filename, "rb")
data = f.read()
print u"%d bytes in %s" % (len(data), filename)

...I first created a file named 指事字.txt in the current directory, with some lines of text in it.

Michiel Overtoom
  • 1,609
  • 13
  • 14
-1

Theres a REALLY good explanation that if you read it will give you alot of understanding. It looks long but its really not and well worth the read: http://nedbatchelder.com/text/unipain/unipain.html#1

But, encode converts a unicode object to a string object. But here you have invoked it on a string object. So python has to convert the string to a unicode object first. So it does the equivalent of

"指事字".decode().encode('utf-8')

But the decode fails because the string isn't valid ascii. That's why you get a complaint about not being able to decode.

kyle heitman
  • 114
  • 6