curl - Python request 上傳文件
問(wèn)題描述
我嘗試用 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)問(wèn)正確的應(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.
問(wèn)題解答
回答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. 前端 - ng-view不能加載進(jìn)模板2. docker容器呢SSH為什么連不通呢?3. android clickablespan獲取選中內(nèi)容4. debian - docker依賴的aufs-tools源碼哪里可以找到啊?5. python - from ..xxxx import xxxx到底是什么意思呢?6. angular.js - angularJS在Android WebView中無(wú)法正常調(diào)后臺(tái)接口7. 關(guān)docker hub上有些鏡像的tag被標(biāo)記““This image has vulnerabilities””8. nignx - docker內(nèi)nginx 80端口被占用9. docker網(wǎng)絡(luò)端口映射,沒(méi)有方便點(diǎn)的操作方法么?10. 請(qǐng)教各位大佬,瀏覽器點(diǎn) 提交實(shí)例為什么沒(méi)有反應(yīng)
