Bad Request 400 when uploading file to Flask

I have a Flask server that looks like this:

import flask, os, werkzeug.utils

UPLOAD_FOLDER = "files/"

def isFileAllowed(file):
    return str("." in file and file.rsplit(".", 1)[1].lower() in ALLOWED_EXTENSIONS)

app = flask.Flask(__name__)

@app.route("/log_receiver", methods = ["POST", "GET"])
def log_receiver():
    if flask.request.method == "POST":
        file = flask.request.files["file"]
        if file and isFileAllowed(file):
            filename = werkzeug.utils.secure_filename(file)
  ["UPLOAD_DIR"], filename))
            return "Sucessfully uploaded"
        return "File couldn't be uploaded"            
    return "404, not found"

if __name__ == '__main__':

I’ve made a test uploader, also in Python, that looks like this:

import requests

def log_uploader():
    with open("log.txt", "rb") as log:
        r ="http://localhost:5000/log_receiver", files={"log.txt": log})

if __name__ == '__main__':

The issue is, whenever I run it, I get a 404 error.

I’ve tried removing the ["file"] in flask.request.files, which removes the 400 error but brings in a 500 error with the following log:

[2022-11-29 16:05:46,547] ERROR in app: Exception on /log_receiver [POST]
Traceback (most recent call last):
  File "/home/day/.local/lib/python3.9/site-packages/flask/", line 2525, in wsgi_app
    response = self.full_dispatch_request()
  File "/home/day/.local/lib/python3.9/site-packages/flask/", line 1822, in full_dispatch_request
    rv = self.handle_user_exception(e)
  File "/home/day/.local/lib/python3.9/site-packages/flask/", line 1820, in full_dispatch_request
    rv = self.dispatch_request()
  File "/home/day/.local/lib/python3.9/site-packages/flask/", line 1796, in dispatch_request
    return self.ensure_sync(self.view_functions[rule.endpoint])(**view_args)
  File "/home/day/Desktop/Coding/Quickraft/Server-side/APIs/", line 17, in log_receiver
    filename = werkzeug.utils.secure_filename(file)
  File "/home/day/.local/lib/python3.9/site-packages/werkzeug/", line 221, in secure_filename
    filename = unicodedata.normalize("NFKD", filename)
TypeError: normalize() argument 2 must be str, not ImmutableMultiDict - - [29/Nov/2022 16:05:46] "POST /log_receiver HTTP/1.1" 500 -

How could I fix this? Thanks in advance,

>Solution :

See again the flask documentation for request.files

Each value in files is a Werkzeug FileStorage object

It is not just the filename but much more than that. The error message is telling you something: That secure_filename() expects a string, but you are passing it something that isn’t a string.

Have a look again at the flask documentation for file uploads:

Leave a Reply