简体   繁体   中英

How to serve file with flask and then download it with curl

So I am trying to make an API with flask and then use it with javascript to display some images on the browser but I am running into problems with unit-testing whether the image sending (through Flask) works properly. This is the flask code I have right now

@api.route("/pictures/<picture_name>")
def send_picture(picture_name):
    """Used to send the requested picture from the pictures folder."""
    return api.send_static_file(picture_name)

And then I am trying to download say for example an image puppy.png with the following command

curl -O --request GET "http://localhost:8000/pictures/tank.png"

I can open the same link on my browser and see the file perfectly so I know the send_static_file function is working properly. But the curl command above downloads the file in such a way that it can't be opened. Is there something else I am doing wrong here?

I am not that familiar with Python and Flask yet...

EDIT : Somehow the same thing works perfectly fine with wget so I am wondering if this is a problem with the way I am making the curl call. Is there something I am doing wrong? Could someone explain how exactly something is downloaded via HTTP? As in are there any special headers to the get request? If not then what is the response formatted like and how is the file stored?

curl -X POST -F **file=@/home/developer/Documents/cloudish_csv/cloudish1.csv** http://127.0.0.1:5000/cloudish/daily_update?api_key=hqjjnynxtgqvkqzowdxpefdv

place your filepath in file field in curl request and use stringio to download the csv calling another extension like above without any file field.Example code attached....

class Clouddish_forecast(Resource):
   def get(self):
       output = StringIO()
       out = pd.read_csv("/home/developer/Downloads/vig.csv",sep=",")
       out.to_csv(output, index=False)
       return Response(output.getvalue(), mimetype="text/csv")

vig.csv will be downloaded by this return statement

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM