-2

I must upload file to flask via curl command. I tried sending using the curl commands from Folder/Files Upload with Flask and it failed to identify the request.

curl --form "fileupload=@filename.txt" http://hostname/resource

And the Python code:

from flask import Flask,abort,render_template,request,redirect,url_for
from werkzeug import secure_filename
import os
app = Flask(__name__)
UPLOAD_FOLDER = './uploads'
app.config['UPLOAD_FOLDER'] = UPLOAD_FOLDER 


@app.route('/upload/',methods = ['POST'])
def upload_file():
    if request.method =='POST':
        file = request.files['file']
        print request.files
        if file:
            filename = secure_filename(file.filename)
            file.save(os.path.join(app.config['UPLOAD_FOLDER'],filename))



if __name__ == '__main__':
    app.run(debug = True)

The data should be saved upon POSTing via CURL command

jww
  • 97,681
  • 90
  • 411
  • 885
  • *"failed to identify the request"* is not a good problem statement. Please provide the exact error message. – jww Jun 06 '19 at 06:22

1 Answers1

0

Well you probably need to return something, without it Flask gives you error try to add

return jsonify({'message':'File uploaded!'})

after that and try it :) Remember to import jsonify from flask!

  • Also im not sure about your curl request, shouldnt be like this? curl -X POST \ http://hostname/resource \ -H 'content-type: multipart/form-data \ -F fileupload=@filename.txt – Mateusz Pydych Jun 06 '19 at 06:37