Python Flask distinct two generics routes
Question:
In python flask I have two routes with approximately the same behaviour to redirect on another service.
The first one is to redirect a generic request to another API.
@app.route('/factory/<path:url>', methods=['GET', 'POST', 'PUT', 'DELETE'])
@cross_origin()
def request_factory(url: str):
resp = manage_factory_message(request)
return app.response_class(
response=resp.content,
mimetype=resp.headers['Content-Type'],
status=resp.status_code
)
And the second redirect also to another service but in charge of the documents.
@app.post('/factory/doc/<path:url>')
@cross_origin()
def upload_file_factory(url):
resp = manage_factory_file_message(request)
return app.response_class(
response=resp.content,
mimetype=resp.headers['Content-Type'],
status=resp.status_code
)
My problem is that the first route is always triggered but never the second as /factory/doc
is include in /factory/<path:url>
Do you have an idea how to manage that ?
I can’t set the /doc
before the /factory
.
I tried to redirect to the second route if the key /doc
was found in the url of the first route but without success.
Answers:
Notice that /factory/doc/ and /factory/ has a common prefix, so the shortest one wins. If you set routes which does not have this relation than both or more can work.
In python flask I have two routes with approximately the same behaviour to redirect on another service.
The first one is to redirect a generic request to another API.
@app.route('/factory/<path:url>', methods=['GET', 'POST', 'PUT', 'DELETE'])
@cross_origin()
def request_factory(url: str):
resp = manage_factory_message(request)
return app.response_class(
response=resp.content,
mimetype=resp.headers['Content-Type'],
status=resp.status_code
)
And the second redirect also to another service but in charge of the documents.
@app.post('/factory/doc/<path:url>')
@cross_origin()
def upload_file_factory(url):
resp = manage_factory_file_message(request)
return app.response_class(
response=resp.content,
mimetype=resp.headers['Content-Type'],
status=resp.status_code
)
My problem is that the first route is always triggered but never the second as /factory/doc
is include in /factory/<path:url>
Do you have an idea how to manage that ?
I can’t set the /doc
before the /factory
.
I tried to redirect to the second route if the key /doc
was found in the url of the first route but without success.
Notice that /factory/doc/ and /factory/ has a common prefix, so the shortest one wins. If you set routes which does not have this relation than both or more can work.