I'm using 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
.
For that I've used the application Postman
, where it works really fine.
On Postman
I've added only the necessary header, which is a Authorization
with some key.
On the body
, Ive choosen form-data
and then the key is an input_image
, and they the image itself.
Now I want to replicate this into Python2.7
, so I've chose to see the Python code on Postman
, which was this one:
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
. 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
?