Make flask only accept POST requests on a route

Viewed 445

I want to build a Flask route which only accepts POST requests.

So far, I have tried achieving this goal by usind the methods parameter of the route decorator.

@app.route("/register")
def register(methods=["POST"]):
    return "register endpoint"

However, when trying to send a GET request to this route with Postman, it simply returns "register endpoint", even though i only added POST to the methods parameter.

How can I make my route ONLY accept POST requests and return an error in all other cases?

1 Answers

You almost got it, the "methods=[]" should be in the decorator :

@app.route("/register", methods=["POST"])
Related