[英]equivalent python requests call to CURL request to upload image
我正在使用Python请求模块,但是无论我尝试上传图像如何,它都能成功,但是在打开/读取图像时会出错。 我将图片编码为base64,设置了内容类型的标头(图片/ png,图片/ jpeg等...)等...
无论如何,我使用CURL执行以下操作,并且可以正常工作:
curl -u test@test.ca:test -H 'Content-Type: image/jpeg' --data-binary @test.jpeg -X POST 'https://test.test.com/api/upload.json?filename=test.jpeg'
这个请求与python中的requests模块(标头等)等效吗?
要重现curl
命令,您无需在base64中对图像进行编码:-- --data-binary @test.jpeg
curl选项test.jpeg
发送test.jpeg
文件:
import requests
r = requests.post('https://example.com/api/upload.json?filename=test.jpeg',
data=open('test.jpeg', 'rb'),
headers={'Content-Type': 'image/jpeg'},
auth=('test@test.ca', 'test')) # username, password
headers = {'Content-Type' : 'image/jpeg'}
params = {'filename' : 'test.jpg'}
r = requests.post("https://test.test.com/api/upload.json",
auth=('user','pw'), headers=headers, params=params)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.