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 嘗試了以下方法,卻得不到正確結果。請問正確的應該怎么寫?
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 測試,curl代碼 和 第二段代碼發出的請求是一樣的,但是 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)
相關文章:
1. MySQL 使用 group by 之后然后 IFNULL(COUNT(*),0) 為什么還是會獲得 null2. wordpress里,這樣的目錄列表是屬于小工具還是啥?3. 我的怎么不顯示啊,話說有沒有QQ群什么的4. mysql 為什么主鍵 id 和 pid 都市索引, id > 10 走索引 time > 10 不走索引?5. 常量在外面不加引號會報錯。6. 一直報這個錯誤7. mysql federated引擎無法開啟8. python如何設置一個隨著系統時間變化的動態變量?9. mysql - 大部分數據沒有行溢出的text字段是否需要拆表10. sublime text3安裝package control失敗
