Method: flask.ctx.after_this_request
Calls: 1, Exceptions: 0, Paths: 1Back
Path 1: 1 calls (1.0)
test_after_request_processing.
test_after_request_processing.
1def after_this_request(f: ft.AfterRequestCallable) -> ft.AfterRequestCallable:
2 """Executes a function after this request. This is useful to modify
3 response objects. The function is passed the response object and has
4 to return the same or a new one.
5
6 Example::
7
8 @app.route('/')
9 def index():
10 @after_this_request
11 def add_header(response):
12 response.headers['X-Foo'] = 'Parachute'
13 return response
14 return 'Hello World!'
15
16 This is more useful if a function other than the view function wants to
17 modify a response. For instance think of a decorator that wants to add
18 some headers without converting the return value into a response object.
19
20 .. versionadded:: 0.9
21 """
22 ctx = _cv_request.get(None)
23
24 if ctx is None:
25 raise RuntimeError(
26 "'after_this_request' can only be used when a request"
27 " context is active, such as in a view function."
28 )
29
30 ctx._after_request_functions.append(f)
31 return f