Question

I have a simple out of the box rails server running and I am trying to figure out how to make it work with the If-Modified-Since header. I am using the following curl request.

curl -I localhost:3000/shows/1 --header 'If-Modified-Since: Thu, 21 Jun 2012 19:16:20 GMT'

I always get 200 OK but I want 304 Not Modified.

When I do this through the browser I get a 304 Not Modified but not with curl.

Am I doing it wrong?

UPDATE

If I run

curl -v -I localhost:3000/shows/1 --header 'If-Modified-Since: Thu, 21 Jun 2012 19:16:20 GMT'

I get:

* About to connect() to localhost port 3000 (#0)
*   Trying 127.0.0.1... connected
* Connected to localhost (127.0.0.1) port 3000 (#0)
> HEAD /shows/1 HTTP/1.1
> User-Agent: curl/7.21.4 (universal-apple-darwin11.0) libcurl/7.21.4 OpenSSL/0.9.8r zlib/1.2.5
> Host: localhost:3000
> Accept: */*
> If-Modified-Since: Thu, 21 Jun 2012 19:16:20 GMT
> 
< HTTP/1.1 200 OK 
< Content-Type: text/html; charset=utf-8
< X-Ua-Compatible: IE=Edge
< Etag: "35ec119e3b6c7ccde9eeee82afcd4ee9"
< Cache-Control: max-age=0, private, must-revalidate
< X-Request-Id: dd365eb0b80f5b9ff62bc16f3dbbe494
< X-Runtime: 0.007800
< Content-Length: 0
< Server: WEBrick/1.3.1 (Ruby/1.9.3/2012-02-16)
< Date: Thu, 21 Jun 2012 19:37:55 GMT
< Connection: Keep-Alive
< Set-Cookie: _spacebarfm-        rails_session=BAh7B0kiD3Nlc3Npb25faWQGOgZFRkkiJWY0NzNjYmFlYzJhNzgzZWRjMjc2MmU4YWFkZDc1OTIwBjsAVEkiEF9jc3JmX3Rva2VuBjsARkkiMWhjU3NvQ2VuRkR1NmJRREV1SmxOMUlaK3VhQU0ycjY2am94cFFmOTVnTTg9BjsARg%3D%3D--f9c756d08065c423cdecba2d710b1b74bf798c12; path=/; HttpOnly

Set-Cookie: _spacebarfm-rails_session=BAh7B0kiD3Nlc3Npb25faWQGOgZFRkkiJWY0NzNjYmFlYzJhNzgzZWRjMjc2MmU4YWFkZDc1OTIwBjsAVEkiEF9jc3JmX3Rva2VuBjsARkkiMWhjU3NvQ2VuRkR1NmJRREV1SmxOMUlaK3VhQU0ycjY2am94cFFmOTVnTTg9BjsARg%3D%3D--f9c756d08065c423cdecba2d710b1b74bf798c12; path=/; HttpOnly

* Connection #0 to host localhost left intact
* Closing connection #0

The rails code for the controller is:

def index
  @shows = Show.all

  respond_to do |format|
    format.html # index.html.erb
    format.json { render json: @shows }
  end
end

The Chrome output is as follows:

Request URL:http://localhost:3000/shows/1.json
Request Method:GET
Status Code:304 Not Modified
Request Headersview source
Accept:text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
Accept-Charset:ISO-8859-1,utf-8;q=0.7,*;q=0.3
Accept-Encoding:gzip,deflate,sdch
Accept-Language:en-US,en;q=0.8
Connection:keep-alive
Cookie:_btest_session=BAh7B0kiD3Nlc3Npb25faWQGOgZFRkkiJTNkNzI3ODc1NjU0M2EyZTViOWY0ZTgzMjU0M2IzYmY4BjsAVEkiEF9jc3JmX3Rva2VuBjsARkkiMUFVZVN0ZHp6L0JRNGVMZDZST2JSNG54Zlg4T3VmSnk4RFNhWXRHbmljK3c9BjsARg%3D%3D--ceb783671372cbcc3ecf971340f2a2e2424c9620; _spacebarfm-rails_session=BAh7B0kiD3Nlc3Npb25faWQGOgZFRkkiJTNhMTcxNjMyNzJkMWRjYzAxY2NiN2EwN2JjMzE5YWFjBjsAVEkiEF9jc3JmX3Rva2VuBjsARkkiMTZZREp5ZC92VTlEQk9oKytCK0hKYW5na1M4bzRPRUM3U3lmWURzU09neWs9BjsARg%3D%3D--793b15b38511bd0805df3a210d7e72152905b8b9
Host:localhost:3000
If-None-Match:"bb012b3a16e7e80ec271b0d234a9b8ce"
User-Agent:Mozilla/5.0 (Macintosh; Intel Mac OS X 10_7_4) AppleWebKit/536.5 (KHTML, like Gecko) Chrome/19.0.1084.56 Safari/536.5
Response Headersview source
Cache-Control:max-age=0, private, must-revalidate
Connection:close
Date:Thu, 21 Jun 2012 21:09:28 GMT
Etag:"bb012b3a16e7e80ec271b0d234a9b8ce"
Server:WEBrick/1.3.1 (Ruby/1.9.3/2012-02-16)
Set-Cookie:_spacebarfm-rails_session=BAh7B0kiD3Nlc3Npb25faWQGOgZFRkkiJTNhMTcxNjMyNzJkMWRjYzAxY2NiN2EwN2JjMzE5YWFjBjsAVEkiEF9jc3JmX3Rva2VuBjsARkkiMTZZREp5ZC92VTlEQk9oKytCK0hKYW5na1M4bzRPRUM3U3lmWURzU09neWs9BjsARg%3D%3D--793b15b38511bd0805df3a210d7e72152905b8b9; path=/; HttpOnly
X-Request-Id:2435168fa20699508e9282c37566c015
X-Runtime:0.005378
X-Ua-Compatible:IE=Edge
Was it helpful?

Solution

You need to use the stale? method to tell Rails that you want to use if-modified-then

You're actually calling the show method. Change it to be:

def show
  @show = Show.find(params[:id])

  if stale?(:etag => @show, :last_modified => @show.updated_at.utc)
    respond_to do |format|
      format.html # index.html.erb
      format.json { render json: @show }
    end
  end
end
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top