Home > Mobile >  Python POST Request with an Image
Python POST Request with an Image

Time:12-08

I'm trying to request to my flask webserver with an image using python and just can't get it to work.

Using cURL it's simple:

curl -XPOST -F "[email protected]" http://127.0.0.1:5001

But in python using my code:

import requests

with open("image.jpg", "rb") as a_file:
  file_dict = {"image.jpg": a_file}
  response = requests.post("http://127.0.0.1:5001", files=file_dict)
  print(response.text)
  print(response.status_code)

I simply get the HTML of the site returned and status 200. Not the JSON that returns using cURL (and is what I want returned).

Any help would be appreciated, Thanks.

CodePudding user response:

you can use follow code

files = {'file': open('image.jpg', 'rb')}
 
r = requests.post('http://127.0.0.1:5001', files=files)
print(r.text)
  • Related