Flask-WTForms provides CSRF protection. It works great when using regular HTML forms, but when using AJAX, the process is less clear. I have a file upload to my form, and I split the process in two with AJAX: the file goes to the upload endpoint, and the rest of the form goes to the submit endpoint. Since the file was sent using AJAX, it does not receive the CSRF token, but I want to protect the upload endpoint from attacks. How can I generate a CSRF token when using AJAX?
@app.route('/submit', methods=["GET","POST"]) @login_required def submit(): form = MyForm() if request.method == "POST" and form.validate():
source share