2

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 ?

Noor
  • 19,638
  • 38
  • 136
  • 254
  • Looks like this does what you need: [Capture arbitrary path in flask route](https://stackoverflow.com/questions/15117416/capture-arbitrary-path-in-flask-route) – sql_knievel Oct 29 '17 at 16:52

2 Answers2

6

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:

Capture Arbitrary Path in Flask Route

sql_knievel
  • 1,199
  • 1
  • 13
  • 26
1

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>.

Dušan Maďar
  • 9,269
  • 5
  • 49
  • 64