response.body not available in after filter (Sinatra 1.x)

80 views
Skip to first unread message

Karel Minarik

unread,
Feb 24, 2010, 4:30:23 AM2/24/10
to sinatrarb
Hi,

in Sinatra 1.x, the new after filter feature does not have access to
response.body (to read or modify). Given this simple app:

require 'rubygems'
require 'sinatra'

after { p response }

get('/') { etag "abc"; "HELLO" }

The response.body comes out as []:

#<Sinatra::Response:0x188bee4 @body=[], ..., @header={"ETag"=>"\"abc
\"", ...>

Notice that the headers set in route block are available. I don't know
if this is expected behaviour or not -- there are no tests for this in
the test suite, but it seems counter-intuitive to me, and the the
README states that:

> After filter are evaluated after each request within the context of the
> request and can also modify the request and response.

Can someone confirm what's the real meaning?

Thanks!,

Karel

P.S.
(My use-case would be to automatically set ETag in an after, globally
in the application -- doing MD5 of the body etc.)

Ben Lovell

unread,
Feb 24, 2010, 5:00:06 AM2/24/10
to sina...@googlegroups.com
On 24 February 2010 09:30, Karel Minarik <karel....@gmail.com> wrote:
Hi,

in Sinatra 1.x, the new after filter feature does not have access to
response.body (to read or modify). Given this simple app:

 require 'rubygems'
 require 'sinatra'

 after { p response }

 get('/') { etag "abc";  "HELLO" }

The response.body comes out as []:

#<Sinatra::Response:0x188bee4 @body=[], ..., @header={"ETag"=>"\"abc
\"", ...>

Notice that the headers set in route block are available. I don't know
if this is expected behaviour or not -- there are no tests for this in
the test suite, but it seems counter-intuitive to me, and the the
README states that:

> After filter are evaluated after each request within the context of the
> request and can also modify the request and response.

Can someone confirm what's the real meaning?


For what it's worth, I'm not seeing this behaviour in the gem I just built from the latest source. I can read the body in the after filter.

Cheers,
Ben 

Karel Minarik

unread,
Feb 24, 2010, 5:52:15 AM2/24/10
to sinatrarb
Hmm, that is weird, 'cause when I require Sinatra from the Git clone
explicitely (messing Sinatra::VERSION so I *know* I am not loading
from gems), I still get empty body:

require 'rubygems'
require 'sinatra-sinatra/lib/sinatra'
after { p response }
get('/') { "HELLO

=> #<Sinatra::Response:0x18a3544 @length=0, @header={"Content-
Type"=>"text/html"}, ..., @body=[], @status=200, @block=nil>

Karel

On Feb 24, 11:00 am, Ben Lovell <benjamin.lov...@gmail.com> wrote:

Karel Minarik

unread,
Feb 24, 2010, 6:28:08 AM2/24/10
to sinatrarb
... and it doesn't work even when I build and install the gem from the
source.

Ben, could you please add a test for it and run it?

http://gist.github.com/313341

Because this fails for me. Again, I don't know what's the expected
behaviour -- but I'd guess I should be able to read/modify the
response.body, _or_ the docs need t be changed....

Thanks,

Karel

Travis Bell

unread,
Feb 24, 2010, 11:03:33 AM2/24/10
to sina...@googlegroups.com
I knew I wasn't going crazy! I posted about this a few days ago and no one replied. response.body, body (helper) and response.length are all empty or nil for me with the latest versions as well. Like you, I am not positive if they should work and are broken, or if it's been removed.

*follows with interest*


--
Travis Bell







--
You received this message because you are subscribed to the Google Groups "sinatrarb" group.
To post to this group, send email to sina...@googlegroups.com.
To unsubscribe from this group, send email to sinatrarb+...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/sinatrarb?hl=en.


Reply all
Reply to author
Forward
0 new messages