Forums

Yet another Flask 405 error on POST and PUT

Hi,

I have written my first Flask app and everything works fine with GET requests but POST and PUT are returning 405 errors (checked with browser and curl -X POST)

App has been reloaded several time and checked the latest version is running by changing the return value on GET requests.

86.98.213.164 - - [24/Oct/2016:07:04:12 +0000] "GET / HTTP/1.1" 200 9 "-" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_0) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/53.0.2785.143 Safari/537.36" "86.98.213.164"
86.98.213.164 - - [24/Oct/2016:07:06:17 +0000] "PUT / HTTP/1.1" 405 178 "-" "curl/7.49.0" "86.98.213.164"
23.227.37.104 - - [24/Oct/2016:07:11:40 +0000] "POST / HTTP/1.1" 405 178 "-" "Ruby" "23.227.37.104"
86.98.213.164 - - [24/Oct/2016:07:15:00 +0000] "PUT / HTTP/1.1" 405 178 "-" "curl/7.49.0" "86.98.213.164"
86.98.213.164 - - [24/Oct/2016:07:15:09 +0000] "GET / HTTP/1.1" 200 9 "-" "curl/7.49.0" "86.98.213.164"
86.98.213.164 - - [24/Oct/2016:07:16:06 +0000] "GET / HTTP/1.1" 200 9 "-" "curl/7.49.0" "86.98.213.164"

Code as follows. Nothing of note to my mind in the access or error log, except the 405 recorded in the access log:-

from flask import Flask

app = Flask(__name__)

@app.route('/', methods=['GET', 'POST', 'PUT'])
def main():
    return "Done sir!"

PUT and POST require a payload and GET does not. Are you including the payload in your test requests?

Tried test request using following command same 405 response.

curl -H "Content-Type: application/json" -X POST -d '{"a":"xyz","b":"xyz"}' http://mattslight.pythonanywhere.com/

There's something else going on, here: I copied the code from your first post into a Flask app, and hit it with the curl command in the post above, and it worked perfectly.

Thank you Glen,

Any idea what it could be? I started on a free tier app - now on a paid. Could that affect things?

Should I destroy the app and start fresh?

Thanks, Matthew

OK - very odd, I deleted the web app and all the pyc files and pycache folders and it now appears to be working. Glad it wasn't my crappy app programming... ;0

M

on a side note, httpie is great alternative to curl

Love it! thank you

mattslight@macbook:~$ http POST mattslight.pythonanywhere.com
HTTP/1.1 200 OK
Connection: keep-alive
Content-Length: 9
Content-Type: text/html; charset=utf-8
Date: Tue, 25 Oct 2016 11:07:44 GMT
Server: openresty/1.9.15.1
X-Clacks-Overhead: GNU Terry Pratchett

Done sir!