MCPcopy
hub / github.com/pallets/flask / after_this_request

Function after_this_request

src/flask/ctx.py:117–149  ·  view source on GitHub ↗

Executes a function after this request. This is useful to modify response objects. The function is passed the response object and has to return the same or a new one. Example:: @app.route('/') def index(): @after_this_request def add_header(res

(
    f: ft.AfterRequestCallable[t.Any],
)

Source from the content-addressed store, hash-verified

115
116
117def after_this_request(
118 f: ft.AfterRequestCallable[t.Any],
119) -> ft.AfterRequestCallable[t.Any]:
120 """Executes a function after this request. This is useful to modify
121 response objects. The function is passed the response object and has
122 to return the same or a new one.
123
124 Example::
125
126 @app.route('/')
127 def index():
128 @after_this_request
129 def add_header(response):
130 response.headers['X-Foo'] = 'Parachute'
131 return response
132 return 'Hello World!'
133
134 This is more useful if a function other than the view function wants to
135 modify a response. For instance think of a decorator that wants to add
136 some headers without converting the return value into a response object.
137
138 .. versionadded:: 0.9
139 """
140 ctx = _cv_request.get(None)
141
142 if ctx is None:
143 raise RuntimeError(
144 "'after_this_request' can only be used when a request"
145 " context is active, such as in a view function."
146 )
147
148 ctx._after_request_functions.append(f)
149 return f
150
151
152F = t.TypeVar("F", bound=t.Callable[..., t.Any])

Callers

nothing calls this directly

Calls 1

getMethod · 0.45

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…