jat
jat

Reputation: 106

Flask 405 on POST even when POST is present

This is my code, a very simple program. Even though POST is present, server is showing 405 on POST request. I tried Postman, http-prompt but the result is same "405". On sending OPTIONS request to server, only GET, HEAD and OPTIONS are shown as allowed methods.Even POST request through a html form is showing 405, of course because server does not even have POST as allowed method despite POST being present in methods kwarg.


@app.route('/')
def index(methods=['GET', 'POST']):
    if request.method == 'GET':
        return render_template('index.html')
    else:
        return 'POST'

index.html is contains a simple HTML heading.

Upvotes: 0

Views: 2119

Answers (1)

Ajax1234
Ajax1234

Reputation: 71471

The methods parameter values should be set in the route wrapper. Also, it is generally cleaner to check if the request is a POST first:

@app.route('/', methods=['GET', 'POST'])
def index():
   if flask.request.method == 'POST':
      return 'POST'
   return render_template('index.html')

Upvotes: 6

Related Questions