Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting 405 Method Not Allowed while using POST method in bottle

Tags:

python

bottle

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 ?

like image 866
Hitul Mistry Avatar asked Oct 09 '12 10:10

Hitul Mistry


People also ask

How do I fix 405 Method not allowed in Postman?

If you are certain you need a POST request, chances are, you're just using the wrong endpoint on the server. Again, this can only be solved if you have proper documentation to show what methods are supported for each endpoint.

Why post method is not allowed?

The Request Method' POST' Not Supported error is caused by a mismatch of the web browser configuration and the browser's URL format. In this case, the browser sends a URL request, the web server receives and recognizes the URL but cannot execute commands or grant access to the requested page.


1 Answers

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

like image 66
Marboni Avatar answered Sep 21 '22 12:09

Marboni