Rails 3.1 – Sending Content-Length header using middleware – Rack::ContentLength

Some of applications need to send content-length header in every request. For example, when sending something to a mobile device - it would be nice to tell this device how much data are we sending, so the mobile user could see real progress bar.

How to do it in Ruby on Rails 3.1? Well, here you have a list of rack middleware. There is one called Rack::ContentLength, which we will be using. To do so, just include following line into application.rb:

    config.middleware.use Rack::ContentLength

There is a lot interesting stuff going on in rack middleware. For example you can also see this Railscast for more details.

Here example of full response header containing content-length:

Date	Sun, 04 Sep 2011 12:46:46 GMT
Server	Apache/2.2.14 (Debian)
X-Powered-By	Phusion Passenger (mod_rails/mod_rack) 2.2.11
Etag	"a464ed7f54b5637d393232d9dbd40523"
X-UA-Compatible	IE=Edge,chrome=1
X-Rack-Cache	miss
X-Runtime	0.039739
Cache-Control	must-revalidate, private, max-age=0
Content-Length	888
Status	200
Keep-Alive	timeout=15, max=100
Connection	Keep-Alive
Content-Type	application/json; charset=utf-8

Categories: Rails, Ruby

1 Comment

  1. Is there a way of doing this for an individual request?

Leave a Reply

Your email address will not be published. Required fields are marked *


Copyright © 2024 Closer to Code

Theme by Anders NorenUp ↑