Rack middleware is a way to filter a request and response coming into your application. In this episode I show how to modify the response body using middleware.
Special thanks to Josh Peek for answering some questions I had on this topic.
Update: making the code below thread safe by duplicating self on call.
Note: as wldock and remi pointed out in the comments, it is important to change the headers["Content-Length"] value to reflect the new body length. See their comments for details.