J
J
Jekson2019-02-18 11:41:27
Flask
Jekson, 2019-02-18 11:41:27

How to upload file via flask rest-api?

I'm using the standard file upload feature from the Flask documentation

@app.route('/', methods=['GET', 'POST'])
def index():
    if request.method == 'POST':
        if 'file' not in request.files:
            flash('No file part')
            return redirect(request.url)
        file = request.files['file']
        if file.filename == '':
            flash('NO selected file')
            return redirect(request.url)
        print(file.filename)
        if not allowed_file(file.filename):
            flash("Only JPG file is used")
            return redirect(request.url)
        if file and allowed_file(file.filename):
            filename = secure_filename(file.filename)
            file.save(os.path.join(app.config['UPLOAD_FOLDER'], filename))
            img = (os.path.join(app.config['UPLOAD_FOLDER'], filename))
            _, count = performDetect(imagePath=img)
            return redirect(url_for("result", count=count))
    return render_template("index.html")

If you use a browser as a client, then everything works. Now I'm testing uploading via Postman, and when sending I get No file part
5c6a6f0ff1395324470456.png
Why is this happening? Is it even possible to use this way of uploading files in rest api?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
P
planc, 2019-02-18
@Lepilov

in key add the name of the field, in your case: file

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question