Etag in Rails

3 09 2007

What is an ETag and How does it work??

When a request is made for the first time then during page render, etag header is automatically inserted on 200 OK responses. The etag is calculated using MD5 of the response body.
On subsequent requests the etag is send along as request.headers[‘HTTP_IF_NONE_MATCH’] and compared with response.headers[‘ETAG’] . If they are equal then the response is changed to a 304 Not Modified and the response body is set to an empty string.

So how does that help??

  • ETag is used to expire the browser caching and for saving transfer of course!!
  • aggregators can tell which version of feed they have and thus server sends data only if some newer version is there.
  • no more cache based on page expiration.

A ruby snippet for showing ETags

require 'net/http'
# my rails app running on edge rails
http ="localhost", 3000)

# /controller/action
data = http.get2("/say")
p data.body
p data.header.each{|k,v| p "#{k} = #{v}"}

data2 = http.request_get("/say", {'If-None-Match' => data.header['etag']})
p data2.body
p data2.header.each{|k,v| p "#{k} = #{v}"}
# 304 not modified n body empty coz of no change in content




One response

14 06 2014
exhaust system manufacture

Do you have any video of that? I’d want to find out some additional information.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: