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. docker gitlab 如何git clone?2. dockerfile - [docker build image失败- npm install]3. javascript - c#如何向js传值4. java - mybatis怎么实现在数据库中有就修改,没有就添加5. node.js - mongoDB使用$gte的问题6. 我在centos容器里安装docker,也就是在容器里安装容器,报错了?7. java中关于直接插入排序遇到的问题。8. node.js - nodejs和前端JavaScript 字符串处理结果不一样是什么原因?9. javascript - npm run build后调用api返回index.html10. nignx - docker内nginx 80端口被占用
