0
votes

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.

  • I am on a Mac Machine(High Sierra)
  • Tried using both gunicorn and inbuilt flask server.
  • Tried using Postman, http-prompt


@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.

1

1 Answers

6
votes

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')