-1

I wrote a small flask server. The purpose is to upload files. The important part, is that this is a server which has only a REST API and doesn't render html and template files.

This is the code:

from flask import Flask, render_template, request, redirect, url_for

app = Flask(__name__)

@app.route('/')
def index():
    return render_template('index.html')

@app.route('/', methods=['POST'])
def upload_file():
    uploaded_file = request.files['file']
    if uploaded_file.filename != '':
        uploaded_file.save(uploaded_file.filename)
    return redirect(url_for('index'))
    
if __name__ == "__main__":
    app.run(debug=True)

I triggered this server using curl:

curl -X POST -d file=/home/user1/Desktop/STEP_files/MyFile.txt 127.0.0.1:5000

However, i got this back:

  File "/home/user1/anaconda3/envs/flaskTest/lib/python3.9/site-packages/werkzeug/datastructures.py", line 442, in __getitem__
    raise exceptions.BadRequestKeyError(key)
werkzeug.exceptions.BadRequestKeyError: 400 Bad Request: The browser (or proxy) sent a request that this server could not understand.

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/home/user1/anaconda3/envs/flaskTest/lib/python3.9/site-packages/flask/app.py", line 2464, in __call__
    return self.wsgi_app(environ, start_response)
  File "/home/user1/anaconda3/envs/flaskTest/lib/python3.9/site-packages/flask/app.py", line 2450, in wsgi_app
    response = self.handle_exception(e)
  File "/home/user1/anaconda3/envs/flaskTest/lib/python3.9/site-packages/flask/app.py", line 1867, in handle_exception
    reraise(exc_type, exc_value, tb)
  File "/home/user1/anaconda3/envs/flaskTest/lib/python3.9/site-packages/flask/_compat.py", line 39, in reraise
    raise value
  File "/home/user1/anaconda3/envs/flaskTest/lib/python3.9/site-packages/flask/app.py", line 2447, in wsgi_app
    response = self.full_dispatch_request()
  File "/home/user1/anaconda3/envs/flaskTest/lib/python3.9/site-packages/flask/app.py", line 1952, in full_dispatch_request
    rv = self.handle_user_exception(e)
  File "/home/user1/anaconda3/envs/flaskTest/lib/python3.9/site-packages/flask/app.py", line 1821, in handle_user_exception
    reraise(exc_type, exc_value, tb)
  File "/home/user1/anaconda3/envs/flaskTest/lib/python3.9/site-packages/flask/_compat.py", line 39, in reraise
    raise value
  File "/home/user1/anaconda3/envs/flaskTest/lib/python3.9/site-packages/flask/app.py", line 1950, in full_dispatch_request
    rv = self.dispatch_request()
  File "/home/user1/anaconda3/envs/flaskTest/lib/python3.9/site-packages/flask/app.py", line 1936, in dispatch_request
    return self.view_functions[rule.endpoint](**req.view_args)
  File "/home/user1/Desktop/flaskServer/server.py", line 11, in upload_file
    uploaded_file = request.files['file']
  File "/home/user1/anaconda3/envs/flaskTest/lib/python3.9/site-packages/flask/debughelpers.py", line 100, in __getitem__
    raise DebugFilesKeyError(request, key)
flask.debughelpers.DebugFilesKeyError: You tried to access the file "file" in the request.files dictionary but it does not exist.  The mimetype for the request is "application/x-www-form-urlencoded" instead of "multipart/form-data" which means that no file contents were transmitted.  To fix this error you should provide enctype="multipart/form-data" in your form.

The browser instead transmitted some file names. This was submitted: "/home/user1/Desktop/STEP_files/MyFile.txt"

The error messages are not very straightforward to me... I programmed the server by the book (keep in mind, this is my first flask server). Not sure what went wrong, or what did i do wrong....

EDIT: It seems like i am able to send the file succesfully with curl, but the file is not saved in the folder that the server exists...

EDIT2: If i do curl -X POST -F 'file=/home/user1/Desktop/STEP_files/MyFile.txt' 127.0.0.1:5000 as Mindslave suugested, i get:

127.0.0.1 - - [02/Feb/2021 16:49:37] "POST / HTTP/1.1" 500 -
Traceback (most recent call last):
  File "/home/user1/anaconda3/envs/flaskTest/lib/python3.9/site-packages/flask/app.py", line 2464, in __call__
    return self.wsgi_app(environ, start_response)
  File "/home/user1/anaconda3/envs/flaskTest/lib/python3.9/site-packages/flask/app.py", line 2450, in wsgi_app
    response = self.handle_exception(e)
  File "/home/user1/anaconda3/envs/flaskTest/lib/python3.9/site-packages/flask/app.py", line 1867, in handle_exception
    reraise(exc_type, exc_value, tb)
  File "/home/user1/anaconda3/envs/flaskTest/lib/python3.9/site-packages/flask/_compat.py", line 39, in reraise
    raise value
  File "/home/user1/anaconda3/envs/flaskTest/lib/python3.9/site-packages/flask/app.py", line 2447, in wsgi_app
    response = self.full_dispatch_request()
  File "/home/user1/anaconda3/envs/flaskTest/lib/python3.9/site-packages/flask/app.py", line 1952, in full_dispatch_request
    rv = self.handle_user_exception(e)
  File "/home/user1/anaconda3/envs/flaskTest/lib/python3.9/site-packages/flask/app.py", line 1821, in handle_user_exception
    reraise(exc_type, exc_value, tb)
  File "/home/user1/anaconda3/envs/flaskTest/lib/python3.9/site-packages/flask/_compat.py", line 39, in reraise
    raise value
  File "/home/user1/anaconda3/envs/flaskTest/lib/python3.9/site-packages/flask/app.py", line 1950, in full_dispatch_request
    rv = self.dispatch_request()
  File "/home/user1/anaconda3/envs/flaskTest/lib/python3.9/site-packages/flask/app.py", line 1936, in dispatch_request
    return self.view_functions[rule.endpoint](**req.view_args)
  File "/home/user1/Desktop/flaskServer/server.py", line 11, in upload_file
    uploaded_file = request.files['file']
  File "/home/user1/anaconda3/envs/flaskTest/lib/python3.9/site-packages/werkzeug/datastructures.py", line 442, in __getitem__
    raise exceptions.BadRequestKeyError(key)
werkzeug.exceptions.BadRequestKeyError: 400 Bad Request: The browser (or proxy) sent a request that this server could not understand.
KeyError: 'file'

And if i do curl -X POST -d "file=@/home/user1/Desktop/STEP_files/MyFile.txt" 127.0.0.1:5000

i get:

127.0.0.1 - - [02/Feb/2021 16:53:11] "POST / HTTP/1.1" 500 -
Traceback (most recent call last):
  File "/home/user1/anaconda3/envs/flaskTest/lib/python3.9/site-packages/flask/debughelpers.py", line 96, in __getitem__
    return oldcls.__getitem__(self, key)
  File "/home/user1/anaconda3/envs/flaskTest/lib/python3.9/site-packages/werkzeug/datastructures.py", line 442, in __getitem__
    raise exceptions.BadRequestKeyError(key)
werkzeug.exceptions.BadRequestKeyError: 400 Bad Request: The browser (or proxy) sent a request that this server could not understand.

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/home/user1/anaconda3/envs/flaskTest/lib/python3.9/site-packages/flask/app.py", line 2464, in __call__
    return self.wsgi_app(environ, start_response)
  File "/home/user1/anaconda3/envs/flaskTest/lib/python3.9/site-packages/flask/app.py", line 2450, in wsgi_app
    response = self.handle_exception(e)
  File "/home/user1/anaconda3/envs/flaskTest/lib/python3.9/site-packages/flask/app.py", line 1867, in handle_exception
    reraise(exc_type, exc_value, tb)
  File "/home/user1/anaconda3/envs/flaskTest/lib/python3.9/site-packages/flask/_compat.py", line 39, in reraise
    raise value
  File "/home/user1/anaconda3/envs/flaskTest/lib/python3.9/site-packages/flask/app.py", line 2447, in wsgi_app
    response = self.full_dispatch_request()
  File "/home/user1/anaconda3/envs/flaskTest/lib/python3.9/site-packages/flask/app.py", line 1952, in full_dispatch_request
    rv = self.handle_user_exception(e)
  File "/home/user1/anaconda3/envs/flaskTest/lib/python3.9/site-packages/flask/app.py", line 1821, in handle_user_exception
    reraise(exc_type, exc_value, tb)
  File "/home/user1/anaconda3/envs/flaskTest/lib/python3.9/site-packages/flask/_compat.py", line 39, in reraise
    raise value
  File "/home/user1/anaconda3/envs/flaskTest/lib/python3.9/site-packages/flask/app.py", line 1950, in full_dispatch_request
    rv = self.dispatch_request()
  File "/home/user1/anaconda3/envs/flaskTest/lib/python3.9/site-packages/flask/app.py", line 1936, in dispatch_request
    return self.view_functions[rule.endpoint](**req.view_args)
  File "/home/user1/Desktop/flaskServer/server.py", line 11, in upload_file
    uploaded_file = request.files['file']
  File "/home/user1/anaconda3/envs/flaskTest/lib/python3.9/site-packages/flask/debughelpers.py", line 100, in __getitem__
    raise DebugFilesKeyError(request, key)
flask.debughelpers.DebugFilesKeyError: You tried to access the file "file" in the request.files dictionary but it does not exist.  The mimetype for the request is "application/x-www-form-urlencoded" instead of "multipart/form-data" which means that no file contents were transmitted.  To fix this error you should provide enctype="multipart/form-data" in your form.

The browser instead transmitted some file names. This was submitted: "@/home/user1/Desktop/STEP_files/Assembly_Test.step"
user1584421
  • 3,499
  • 11
  • 46
  • 86
  • try 'curl -X POST -F 'file=/home/user1/Desktop/STEP_files/MyFile.txt' 127.0.0.1:5000' – Marius Kimmina Feb 02 '21 at 14:28
  • Alternative to that, it should also work to prepend an @ to the filename, that would then be: curl -X POST -d "file=@/home/user1/Desktop/STEP_files/MyFile.txt" 127.0.0.1:5000 – Marius Kimmina Feb 02 '21 at 14:32
  • @Mindslave Thank you. I tried the things you suggested. But they did not work. I updated my question with your approaches and the errors i got. – user1584421 Feb 02 '21 at 14:54
  • Hm, -F seems to get us closer, since it does not complain about the mimetype anymore. Can you also try using the @ syntax with -F? That is curl -X POST -F file=@/home/user1/Desktop/STEP_files/MyFile.txt 127.0.0.1:5000 – Marius Kimmina Feb 02 '21 at 15:00
  • @Mindslave You are a god! It worked! Thank you very much! Please do a normal answer so i can upvote and mark it as the selected answer! – user1584421 Feb 02 '21 at 15:06

1 Answers1

1

The Problem here was the Curl command, not the Flask implementation, use -F to send a multipart/form-data request and the @ snytax to acctually send a file and not the filename as a String.

curl -X POST -F file=@/home/user1/Desktop/STEP_files/MyFile.txt 127.0.0.1:5000
Marius Kimmina
  • 999
  • 6
  • 13