Home > Enterprise >  Sending a file via curl to flask, flask shows no FileStorage objects in request.files
Sending a file via curl to flask, flask shows no FileStorage objects in request.files

Time:09-22

I am running a server with Flask using the following code:

from flask import Flask, flash, request, redirect, url_for
app = Flask(__name__)

@app.route('/', methods=['POST'])
def upload_file():
    # check if the post request has the file part
    print(request.files.__dict__)
    print(request.form.__dict__)
    print(request.data)
    return ""

app.run(host='0.0.0.0', port=5000);

I run it with the command "python server2.py" where server2.py is the server code above. I am in a virtual environment with Flask installed.

In a separate shell on the same machine, at the command line, I run:

curl -F [email protected]  http://192.168.1.25:5000

I expect to see something present and printed out in that first print statement, the one for request.files.__dict__.

Instead, I see this output:

 * Serving Flask app 'server2' (lazy loading)
 * Environment: production
   WARNING: This is a development server. Do not use it in a production deployment.
   Use a production WSGI server instead.
 * Debug mode: off
 * Running on all addresses.
   WARNING: This is a development server. Do not use it in a production deployment.
 * Running on http://192.168.1.25:5000/ (Press CTRL C to quit)
{}
{}
b''
192.168.1.25 - - [09/Sep/2021 18:58:41] "POST / HTTP/1.1" 200 -

This says to me that curl communicated to my server, and the server accepted the communication (200), but, the server didn't put anything in request.files.__dict__.

Can anyone help me understand why this short Flask program isn't communicating with curl in the way I expect?

Thanks!

CodePudding user response:

Everything works as expected, except the way you try to read the data.

You need to access the data e.g. via request.files["onlyFile"].

request.files is not a standard Python dict, but an ImmutableMultiDict.

How do I know? I set a breakpoint and inspected the state of request.

That is not too hard - I did a 5 min lightning talk how to debug Flask applications:

https://www.youtube.com/watch?v=Fxkco-gS4S8

  • Related