Starlette: Response Handler

Created on 20 Aug 2019  路  4Comments  路  Source: encode/starlette

I have a case where I'd like to do the following.

@app.on_event('response')
def response_handler(response):
    return JSONResponse(response[1], status_code=response[0])

@app.route('/')
def root(request):
    return 200, { 'hello': 'world' }

Is this sort of response handler possible?

Most helpful comment

@dgrahn if you really wanted to follow that pattern, you could write a decorator to place in between @app.route('/') and def root(request): that takes the result and packs it into the json response.

If you didn't want to place that decorator everywhere, you could override the app.route method to do it for you.

All 4 comments

No, there's nothing like that available.
You'll need to return response instances from endpoints.

@dgrahn if you really wanted to follow that pattern, you could write a decorator to place in between @app.route('/') and def root(request): that takes the result and packs it into the json response.

If you didn't want to place that decorator everywhere, you could override the app.route method to do it for you.

Would this be something you guys would be interested in as an enhancement?

Not currently, no. I think it's a reasonable design constraint for us to impose, in order to provide consistency.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

berislavlopac picture berislavlopac  路  5Comments

DrPyser picture DrPyser  路  6Comments

koddr picture koddr  路  6Comments

Ekuzkamaza picture Ekuzkamaza  路  3Comments

cw-andrews picture cw-andrews  路  4Comments