Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

308 redirect when using POST to upload a file to Flask application

I am building a small server application in Flask as part of a project I am working on. Part of the functionality is the ability to upload a small file of instructions for one of the key injectors to download. Each key injector has a name (''') which corresponds to a client and the file is uploaded to the server via a POST request.

The following listing is the Flask code.

@app.route('/upload/instructions/<ducky_name>/', methods = ['POST'])
def upload_Instruction(ducky_name):
    file = request.files()
    path = os.getcwd() +"/files/" + ducky_name
    with open(path, "w") as f:
        f.write(file)
        print(f)
        f.close()
        return "Success"

And I am using this curl command to upload the file.

curl -X POST -d @test http://127.0.0.1:5000/upload/instructions/test1

I then get a 308 redirect, and the file is not uploaded. This is the first time I've dealt with uploading files as a POST in this way, and the also the first time I've used Flask.

Thanks

like image 395
MartinHarvey Avatar asked Dec 18 '22 13:12

MartinHarvey


1 Answers

The URL you use in the curl request does not have the trailing slash as in your Flask route. In this case the framework redirects you to the route with slash (see the documentation entry). So just add the trailing slash:

curl -X POST -d @test http://127.0.0.1:5000/upload/instructions/test1/

Flask uses 308 HTTP response code instead of more common 301 to preserve the request method and body during redirect.

like image 175
Sergey Shubin Avatar answered Jun 08 '23 12:06

Sergey Shubin