Attaching custom header to all responses

40 views
Skip to first unread message

jens.t...@gmail.com

unread,
Nov 6, 2017, 6:49:04 PM11/6/17
to pylons-discuss
Hi, 

This question is more of a curious one as I explore different possible solutions for a problem.

What is the recommended way of adding an X-Myapp-Didum header to every response returned from the server? It looks like there are different ways to achieve that:
  • Change the Response factory: inherit from the Pyramid Response class and add the header in __init__;
  • Use a Response callback which adds the header whenever a response is returned from a view function;
  • A Finished callback, like the Response callback, can add the header to the request.response object.
It looks like the Response factory is the favorable way to go, considering that both callbacks seem to work only on a per-request basis? Or what other ways exist to achieve this?

Thanks!
Jens

Michael Merickel

unread,
Nov 6, 2017, 6:55:41 PM11/6/17
to Pylons
A fourth (and probably best way in this case) is to register a NewResponse subscriber. Another global hook is to add a tween. A good hook that allows certain views to opt-out or opt-in to something is a view deriver. They all affect different parts of the pipeline [1] but a NewResponse subscriber is a pretty good one here for something that affects every request.


--
You received this message because you are subscribed to the Google Groups "pylons-discuss" group.
To unsubscribe from this group and stop receiving emails from it, send an email to pylons-discuss+unsubscribe@googlegroups.com.
To post to this group, send email to pylons-discuss@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/pylons-discuss/2f333583-13ff-4587-a11d-b81e47948109%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply all
Reply to author
Forward
0 new messages