2

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

3 Answers 3

1

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

2
  • Hi @Jrmyy Thanks for that. But apparentely the web API also requests the payload I've also placed above. Is there any way to use also that? If I just use as data=payload, it complains that the payload can't be a string.
    – waas1919
    Mar 26, 2018 at 9:40
  • @waas1919, if you need to put the payload, maybe something like that could work : with open('my_image.jpg', "rb") as image_file: payload = base64.b64encode(image_file.read()) And then you can use a payload, because it will be bytes (if you want, you can try to convert to bytes the payload you have in your example)
    – Jrmyy
    Mar 26, 2018 at 11:46
0

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')}
0

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)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.