I found a exception when i use NestableBlueprint. It raise FormDataRoutingRedirect Exception.
And I found this exception again where I run a simple test demo.
test demo:
@user.route('/v1/', methods=['GET', 'POST'])
def user_test():
print 'test'
return 'test'
get http://192.168.2.103:7700/v1 -> successed
get http://192.168.2.103:7700/v1/ -> successed
post http://192.168.2.103:7700/v1/ -> successed
post http://192.168.2.103:7700/v1 -> raise FormDataRoutingRedirect:
File "/home/ghostbod/python/hmt_api/venv/lib/python2.7/site-packages/flask/app.py", line 1439, in raise_routing_exception
raise FormDataRoutingRedirect(request)
FormDataRoutingRedirect: A request was sent to this URL (http://192.168.2.103:7700/v1) but a redirect was issued automatically by the routing system to "http://192.168.2.103:7700/v1/". The URL was defined with a trailing slash so Flask will automatically redirect to the URL with the trailing slash if it was accessed without one. Make sure to directly send your POST-request to this URL since we can't make browsers or HTTP clients redirect with form data reliably or without user interaction.
NestableBlueprint test demo
init.py:
from modules.warehouse.bp import warehouse
app.register_blueprint(warehouse, url_prefix='/v1/warehouse')
bp.py:
from wh.views import bp_wh
warehouse = NestableBlueprint('warehouse', name)
warehouse.register_blueprint(bp_wh, url_prefix='/wh')
view.ppy:
@bp_wh.route('/', methods=['POST'])
@oauth.common_require_oauth()
def test(cid, uid):
return "test"
http://192.168.2.103:7700/v1/warehouse/wh/?access_token=NX5fUsTrKR8Gi6AsrWgT0tFQAR5C7w -> successed
http://192.168.2.103:7700/v1/warehouse/wh?access_token=NX5fUsTrKR8Gi6AsrWgT0tFQAR5C7w -> raise FormDataRoutingRedirect(request)
First of all, please format your code properly when posting it here. It is not hard. See https://guides.github.com/features/mastering-markdown/ on how to do it.
I also don't see the problem. When you have a rule for foo/ but try accessing foo instead, it redirects to foo/. However, this is impossible for POST requests because you cannot redirect and force the client to re-send the POST data. So in that case an error is raised. The solution is to use the correct URL, or disable strict_slashes (the name might be different but similar) to disable the redirect.
Thank you. I will format my code Next time.
'url_prefix=/foo' will match a foo rule to foo/ , So i can not use url_prefix when i want to use the post.
All url_prefix does is prefix + whatever - it does not add any special behavior. So if your prefix is /foo and you want a rule in your blueprint for /foo you pass an empty string for rule:
@bp_wh.route('', methods=['POST'])
BTW, you can edit your old post.
Thank you
Most helpful comment
First of all, please format your code properly when posting it here. It is not hard. See https://guides.github.com/features/mastering-markdown/ on how to do it.
I also don't see the problem. When you have a rule for
foo/but try accessingfooinstead, it redirects tofoo/. However, this is impossible for POST requests because you cannot redirect and force the client to re-send the POST data. So in that case an error is raised. The solution is to use the correct URL, or disablestrict_slashes(the name might be different but similar) to disable the redirect.