Python HTTP Post With Upload File And Headers Generated From Postman

- 1 answer

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. Postman example

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 = ""

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)


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.


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?



I think Postman is doing some logic we are not really aware of. But the package requests provide a way to upload images.

files = {'media': open('my_image.jpg', 'rb')}
r =, 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.