Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to send image to Flask server from curl request

I want to send an image by curl to flask server, i am trying this curl command
curl -X POST -F file=image.jpg "http://127.0.0.1:5000/" but it did not work by the way on the server side i handle the image by this code
image = Image.open(request.files['file']) i am trying to read the image using PIL
Is there anyway to do this?
Thanks in advance

like image 724
Ramahi.Amer Avatar asked Jan 14 '17 22:01

Ramahi.Amer


People also ask

How do I send a POST request to Flask API?

Generating POST Requestpost() method is used to generate a POST request. This method has can contain parameters of URL, params, headers and basic authentication. URL is the location for sending the request. Params are the list of parameters for the request.

Can Flask handle HTTP requests?

Flask has different decorators to handle http requests. Http protocol is the basis for data communication in the World Wide Web. Used to send HTML form data to the server. The data received by the POST method is not cached by the server.


1 Answers

This worked for me:

curl -F "[email protected]" http://localhost:5000/

The '@' is important, otherwise you end up with an http error 400 (server could not understand request). I've also dropped the "-X POST" bit as it's unnecessary.

My flask view:

from PIL import Image

@app.route("/", methods=["POST"])
def home():
    img = Image.open(request.files['file'])
    return 'Success!'
like image 172
David Simic Avatar answered Oct 16 '22 08:10

David Simic