[FIXED] 400 http error on all post method of a blueprint

Issue

So I have this project with blueprints added…
Everything works fine, I tested the endpoint. okay okay

2 weeks later I am debugging and testing a task I did in a sprint and I am getting a 400 HTTP error for every request….

Do you have any idea what might have caused the problem?

App files

from my_bp import bp
app.register_blueprint(bp,url_prefix="/test")
 

my_bp files

bp = Blueprint("my_bp",__name__)

@bp.route("/test",methods=["GET","POST"]
Deftest():
return {"test":"helloworld"}
 

Sending a get request via postman works fine, but when I try to send a simple post request without body (or with body) I get a 400 error response…

Thank you

Additional note All other blueprints are working fine but this one is returning 400 on every post request
Using Post-man to send append requests

Solution

So it turns out it’s related to Flask-WTF CSRF protection …..

For anyone who does not know why they can’t get their route running here is the answer.

app.init.py:

...
from flask_wtf.csrf import CSRFProtect
...
...
csrf = CSRFProtect()
...

and after initializing your csrf protection you need to exempt the routes :

from app import csrf
bp = Blueprint("my_bp",__name__)

@csrf.exempt
@bp.route("/test",methods=["GET","POST"]
def test():
return {"test":"helloworld"}

Answered By – Mehrdad Khojastefar

Answer Checked By – Gilberto Lyons (Easybugfix Admin)

Leave a Reply

(*) Required, Your email will not be published