Python Flask在组合GET和POST请求时失败

时间:2016-08-01 10:55:32

标签: python flask

我在Python Flask中编写了一个函数来处理多个api的oauth2回调。

现有的功能是:

@app.route('/external_api/<api>/oauth2callback', methods=['POST', 'GET'])
def gdrive_oauth2callback(api):
    toReturn = external_api.APIs[api]['lib'].oauth2callback(os.path.join(APP_OAUTH, str(api + '_id.json')))
    userSession = UserSession()
    userSession.addUserAuth(api)
    return toReturn

但是这会导致构建错误:

raise BuildError(endpoint, values, method)
BuildError: ('gdrive_oauth2callback', {}, None)

我很困惑为什么会发生这种情况,就像我用字符串&#39; gdrive&#39;替换api变量一样。没有创建错误,它完美地工作。我在两种情况下都以相同的方式调用该函数(example.com/external_api/gdrive/oauth2callback),我想知道Flask是否无法同时处理POST请求和GET请求。同一时间,如果其他人有同样的问题?

完整的错误日志如下:

ERROR:app:Exception on /external_api/gdrive/connect [GET]
Traceback (most recent call last):
File "/app/.heroku/python/lib/python2.7/site-packages/flask/app.py", line 1817, in wsgi_app
response = self.full_dispatch_request()
File "/app/.heroku/python/lib/python2.7/site-packages/flask/app.py", line 1477, in full_dispatch_request
rv = self.handle_user_exception(e)
File "/app/.heroku/python/lib/python2.7/site-packages/flask/app.py", line 1381, in handle_user_exception
reraise(exc_type, exc_value, tb)
File "/app/.heroku/python/lib/python2.7/site-packages/flask/app.py", line 1475, in full_dispatch_request
rv = self.dispatch_request()
File "/app/.heroku/python/lib/python2.7/site-packages/flask/app.py", line 1461, in dispatch_request
return self.view_functions[rule.endpoint](**req.view_args)
File "/app/app/views.py", line 43, in gdrive_connect
toReturn = external_api.APIs[api]['lib'].makeConnection()
File "/app/app/external_api/gdrive/api.py", line 14, in makeConnection
return flask.redirect(flask.url_for('gdrive_oauth2callback'))
File "/app/.heroku/python/lib/python2.7/site-packages/flask/helpers.py", line 312, in url_for
return appctx.app.handle_url_build_error(error, endpoint, values)
File "/app/.heroku/python/lib/python2.7/site-packages/flask/app.py", line 1641, in handle_url_build_error
reraise(exc_type, exc_value, tb)
File "/app/.heroku/python/lib/python2.7/site-packages/flask/helpers.py", line 305, in url_for
force_external=external)
File "/app/.heroku/python/lib/python2.7/site-packages/werkzeug/routing.py", line 1616, in build
raise BuildError(endpoint, values, method)
BuildError: ('gdrive_oauth2callback', {}, None) - at=info method=GET path

如果有人能给我任何指示,那就太棒了!谢谢!

1 个答案:

答案 0 :(得分:0)

我认为烧瓶一定不能失败,如果看一下这个例子,它可以很好地运作,http://flask.pocoo.org/snippets/62/

我试过了:

from flask import Flask
app = Flask(__name__)

@app.route("/<name>",methods=['POST', 'GET'])
def hello(name):
   return "Hello World!"+name

if __name__ == "__main__":
   app.run()

它在发布和获取请求时工作正常。我知道这不是什么,但它表明问题可能不是。