curl - Python request 上傳文件
問題描述
我嘗試用 curl 提交成功
curl --form file=@/home/test/sample.png --form username=test@noreply.com --form password=test --insecure --form lang[0]=cn --form lang[1]=jp --form langs[2]=en https://www.example.com/api
但我用 requests 嘗試了以下方法,卻得不到正確結(jié)果。請(qǐng)問正確的應(yīng)該怎么寫?
data = { ’file’: open(’/home/test/test.png’,’rb’), ’username’: ’test@noreply.com’, ’password’: ’test’, ’lang[0]’: ’cn’, ’lang[1]’: ’jp’, ’lang[2]’: ’en’}r = requests.post(’https://www.example.com/api’, data=data, verify=False)
file = { ’file’: open(’/home/test/test.png’,’rb’) }data = { ’username’: ’test@noreply.com’, ’password’: ’test’, ’lang[0]’: ’cn’, ’lang[1]’: ’jp’, ’lang[2]’: ’en’}r = requests.post(’https://www.example.com/api’, data=data, files=file, verify=False)
另外我用 httpbin 測(cè)試,curl代碼 和 第二段代碼發(fā)出的請(qǐng)求是一樣的,但是 Python 得不到返回的 ID.
問題解答
回答1:files = {’file’: open(’test.png’, ’rb’)}requests.post(url, files=files)
參考 http://www.python-requests.or...
http://www.python-requests.or...
回答2:with open(’filename1’, ’rb’) as f1, open(’filename2’, ’rb’) as f2: files_to_upload = {’filename1’: f1,’filename2’: f2, }response = requests.post(url, files=files_to_upload)
相關(guān)文章:
1. html5 - 只用CSS如何實(shí)現(xiàn)input框的寬度隨框里輸入的內(nèi)容長短自動(dòng)適應(yīng)?2. 人工智能 - python 機(jī)器學(xué)習(xí) 醫(yī)療數(shù)據(jù) 怎么學(xué)3. mysql - 分庫分表、分區(qū)、讀寫分離 這些都是用在什么場(chǎng)景下 ,會(huì)帶來哪些效率或者其他方面的好處4. Python爬蟲如何爬取span和span中間的內(nèi)容并分別存入字典里?5. javascript - 關(guān)于css絕對(duì)定位在ios瀏覽器被橡皮筋遮擋的問題6. c++ - 請(qǐng)問MySQL_Connection::isReadOnly 怎么解決?7. css3 - 微信前端頁面遇到的transition過渡動(dòng)畫的bug8. python - beautifulsoup獲取網(wǎng)頁內(nèi)容的問題9. python - Django內(nèi)使用filter過濾時(shí)間,只認(rèn)年份不認(rèn)月份是怎么回事?10. python - 能通過CAN控制一部普通的家用轎車嗎?
