我需要使用python中的request模塊發佈文件,該模塊在文件名中包含unicode字符。在Python中使用requests.post在文件名中使用Unicode字符發送文件
我使用下面的代碼:
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)
使用上面的代碼,並通過文件名是 「指事字的.exe」 的時候,我得到以下異常:
'ascii' codec can't decode byte 0xc2 in position 26: ordinal not in range(128)
任何幫助真的很感激。
PS:我已經嘗試下面的代碼,它不工作對我來說:
file_up = {'filename': open(file_name.encode('utf-8'), 'rb')}