[英]Python HTTP Post with upload file and headers generated from Postman
I'm using Python 2.7
. 我正在使用
Python 2.7
。
I want to make a HTTP POST
using requests
, where I upload a file and a key that must go in the HTTP Headers
. 我想使用
requests
进行HTTP POST
,在该requests
中上传文件和必须放在HTTP Headers
的密钥。
For that I've used the application Postman
, where it works really fine. 为此,我使用了
Postman
应用程序,它在这里确实可以正常工作。
On Postman
I've added only the necessary header, which is a Authorization
with some key. 在
Postman
我仅添加了必要的标头,即带有某些密钥的Authorization
。 On the body
, Ive choosen form-data
and then the key is an input_image
, and they the image itself. 在
body
,我选择了form-data
,然后键是一个input_image
,它们就是图像本身。
Now I want to replicate this into Python2.7
, so I've chose to see the Python code on Postman
, which was this one: 现在,我想将其复制到
Python2.7
,所以我选择在Postman
上查看Python代码,它就是这样的代码:
import requests
url = "https://foo.com/bar/stuff"
payload = "------WebKitFormBoundary7MA4YDxkTrZu1gW\r\nContent-Disposition: form-data; name=\"input_image\"; filename=\"C:\\Test\\projs\\Supermarket\\doritos.jpeg\"\r\nContent-Type: image/jpeg\r\n\r\n\r\n------WebKitFormBoundary7MA4YDxkTrZu1gW--"
headers = {
'content-type': "multipart/form-data; boundary=----WebKitFormBoundary7MA4YDxkTrZu1gW",
'Authorization': "myAuthorizationKey",
'Cache-Control': "no-cache",
'Postman-Token': "0efwd6e8-051c-4ed5-8d6f-7b1bd135f4d5"
}
response = requests.request("POST", url, data=payload, headers=headers)
print(response.text)
This simply doesn't work. 这根本行不通。 It has the same behaviour as if I didn't send any image using
Postman
. 它具有与我没有使用
Postman
发送任何图像相同的行为。 It looks like the payload string is not being send correctly. 看来负载字符串未正确发送。
Question: 题:
What is wrong with this Postman
auto-generation code in order to send a HTTP POST
with image upload
and with header
at the same time in Python
? 为了在
Python
同时发送带有image upload
和header
的HTTP POST
,此Postman
自动生成代码有什么问题?
I think Postman is doing some logic we are not really aware of. 我认为Postman正在做一些我们并未真正意识到的逻辑。 But the package
requests
provide a way to upload images. 但是打包
requests
提供了一种上传图像的方法。
files = {'media': open('my_image.jpg', 'rb')}
r = requests.post(url, files=files, headers=hearders)
According to the server you are sending the image to, the parameters name, this code might need to be slightly changed. 根据要将图像发送到的服务器(参数名称),此代码可能需要稍作更改。
the only trick works here is your code should be same as you post request in postman, no extra headers need to be added , your post request should look like the same as it is in postman. 唯一有用的技巧是您的代码应与邮递员中的发布请求相同,无需添加额外的标头,您的邮寄请求应与在邮递员中看起来相同。 I could do this by changing my file to an image file and then posting it in my post request.
我可以通过将文件更改为图像文件,然后将其发布到我的发布请求中来做到这一点。
with open('grass-small.png', 'rb') as imageFile:
imageStr = base64.urlsafe_b64encode(imageFile.read())
files = {'document': ('grass-small.png', imageStr ), 'document_type':(None,'grass')}
This worked for me 这对我有用
import requests
url = 'http://iizuka.cs.tsukuba.ac.jp/projects/colorization/web/'
files = {'file': ("my_img_path/myImage.jpeg", open('my_img_path/myImage.jpeg', 'rb'),'image/jpg')}
r = requests.post(url, files=files)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.