'Generic endpoint in Flask [duplicate]
In Flask, is it possible to have a generic endpoint which can serve different requests, I take an example, suppose I have a handler for "/" and I want that all request is handled by this handler to generate a reply. A request like "/person/767" or "/car/324" should also handled by the same endpoint and it is going to generate the reply base on resource requested. Is that possible and if yes, how ?
Solution 1:[1]
If you want an endpoint to literally capture everything after a particular slash, you can use a path placeholder in your route definition.
@app.route('/<path:path>')
A more detailed example in this answer:
Solution 2:[2]
You can register multiple routes to one view function
from flask import Flask
app = Flask(__name__)
@app.route('/')
@app.route('/car/<id>')
@app.route('/person/<id>')
def generic_endpoint(id=None):
...
or, as pointed out by @sql_knievel, use <path:path>.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | |
| Solution 2 | DuĊĦan Ma?ar |
