在瓶子中使用POST方法时不允许405方法

Hit*_*try 6 python bottle

我正在为强制下载开发一个简单的代码现在问题是我在GET方法中没有收到任何错误但在post方法请求中收到错误"405 Method Not Allowed".我的GET方法代码.

@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"
Run Code Online (Sandbox Code Playgroud)

当我用密钥请求它时,它返回我的文件下载,当它是获取方法 http://mydomain.com/down/xyz.pdf?key=tCJVNTh21nEJSekuQesM2A

现在我使用另一个代码来处理POST方法

@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"
Run Code Online (Sandbox Code Playgroud)

现在通过使用此代码我无法处理post方法,即我从服务器获得405 Method Not Allowed错误.

对此有何解决方案?

Mar*_*oni 12

路由器在method参数中只采用一种方法,而不是方法列表.使用几个@route装饰器:

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

查看文档以获取更多信息:http://bottlepy.org/docs/dev/routing.html#routing-order

UPDATE

Recent Bottle版本允许指定方法列表:http://bottlepy.org/docs/dev/api.html#bottle.Bottle.route

  • 实际上,最新版本的Bottle允许使用方法列表:[**方法** - HTTP方法(GET,POST,PUT,...)或要监听的方法列表.(默认:GET)](http://bottlepy.org/docs/dev/api.html#bottle.Bottle.route) (2认同)