python-requests post with unicode filenames
I think maybe there's confusion here on encoding here -
eng_file_name = 'a.txt' # ASCII encoded, by default in Python 2heb_file_name = u'א.txt' # NOT UTF-8 Encoded - just a unicode object
To send the second one to the server what you want to do is this:
requests.post('http://localhost:5000/', files={'file0': open(heb_file_name.encode('utf-8'), 'rb')})
I'm a little surprised that it doesn't throw an error on the client trying to open the file though - you see nothing on the client end indicating an error?
EDIT: An easy way to confirm or deny my idea is of course to print out the contents from inside the client to ensure it's being read properly.
I workaround this issue by manually reading the file with read()
and then posting its contents:
requests.post(upload_url, files={ 'file': ("photo.jpg", open(path_with_unicode_filename, 'rb').read())})
Try this workaround:filename.encode("utf-8").decode("iso-8859-1")
.
Example:
requests.post("https://example.com", files={"file": ("中文filename.txt".encode("utf-8").decode("iso-8859-1"), fobj, mimetype)})
I post this because this is my first result when searching python requests post filename encoding
.
There are lots of RFC standards about Content-Disposition
encoding.And it seems that different programs implement this part differently.
See stackoverflow: lots of RFCs and application tests, RFC 2231 - 4, email.utils.encode_rfc2231.