Getting 405 Method Not Allowed while using POST method in bottle

Hitul Mistry picture Hitul Mistry · Oct 9, 2012 · Viewed 11.6k times · Source

I am developing one simple code for force download now problem is that i'm not getting any error in GET method but getting error "405 Method Not Allowed" in post method request. My code for GET method.

@route('/down/<filename:path>',method=['GET', 'POST'])
    def home(filename):
        key = request.get.GET('key')
        if key == "tCJVNTh21nEJSekuQesM2A":        
            return static_file(filename, root='/home/azoi/tmp/bottle/down/', download=filename)
        else:
    return "File Not Found"

When i request with key it is returning me file for download when it is get method http://mydomain.com/down/xyz.pdf?key=tCJVNTh21nEJSekuQesM2A

Now i used another code for handling POST methods

@route('/down/<filename:path>',method=['GET', 'POST'])
    def home(filename):
        key = request.body.readline()
        if key == "tCJVNTh21nEJSekuQesM2A":        
            return static_file(filename, root='/home/azoi/tmp/bottle/down/', download=filename)
        else:
            return "File Not Found"

Now by using this code i cannot handle post method i.e. i am getting 405 Method Not Allowed error from server.

Any solution for this ?

Answer

Marboni picture Marboni · Oct 9, 2012

Router takes only one method in method parameter, not list of methods. Use several @route decorators instead:

@route('/down/<filename:path>', method='GET')
@route('/down/<filename:path>', method='POST')
def home(filename):
    pass

Check documentation for more information: http://bottlepy.org/docs/dev/routing.html#routing-order

UPDATE

Recent Bottle version allows to specify list of methods: http://bottlepy.org/docs/dev/api.html#bottle.Bottle.route