What does "WARN Could not determine content-length of response body." mean and how to I get rid of it?

Ruby on-RailsRubyWebrick

Ruby on-Rails Problem Overview


Since upgrading to Rails 3.1 I'm seeing this warning message in my development log:

> WARN Could not determine content-length of response body. Set content-length of the response or set Response#chunked = true

What does this mean and how can I remove it? Is it a problem?

Ruby on-Rails Solutions


Solution 1 - Ruby on-Rails

Asked the same question to one of Rails-Core's members:

https://twitter.com/luislavena/status/108998968859566080

And the answer:

https://twitter.com/tenderlove/status/108999110136303617

> ya, it's fine. Need to clean it up, but nothing is being hurt.

Solution 2 - Ruby on-Rails

The following patch solved the problem in my case; no more warnings for me.

204_304_keep_alive.patch

Just edit the file httpresponse.rb at line 205 as shown at the link above; in fact the link shows a correction made to a future release of Ruby.

I'm using rails 3.2.0 on ruby 1.9.3-p0 installed through RVM as a single user. So the location in my case is:

~/.rvm/rubies/ruby-1.9.3-p0/lib/ruby/1.9.1/webrick/httpresponse.rb

The location of the file to be altered differs depending on the type of installation, RVM or not, or even multi-user or single user, so I'm just giving the last part of it:

.../ruby-1.9.3-p0/lib/ruby/1.9.1/webrick/httpresponse.rb

I hope this can be helpful to someone.

EDIT: This is the link to the commit that altered the line in question in the trunk branch of ruby project.

Solution 3 - Ruby on-Rails

Just explicitly adding the Gem to the Gemfile got rid of the warning messages for me:

group :development do
  gem 'webrick', '~> 1.3.1'
end

Solution 4 - Ruby on-Rails

You can also use Thin instead of the default Webrick. Add this to Gemfile

gem 'thin'

then rails s thin will use thin, and the warning will disappear.

Solution 5 - Ruby on-Rails

If you're using .rvm, do this to fix it...

As mentioned by João Soares, all credits to him, this is what you can do if you wan't to get rid of this warning on development.

  1. Use your favorite editor to open this file:

     ~/.rvm/rubies/<ruby-version>/lib/ruby/1.9.1/webrick/httpresponse.rb
    
  2. Go to the line that contains this(for me it was really line 206):

     if chunked? || @header['content-length']
    
  3. Change it, taken from this patch, to this:

     if chunked? || @header['content-length'] || @status == 304 || @status == 204
    
  4. Save the file and eventually restart your rails server

Solution 6 - Ruby on-Rails

This problem has been fixed in Ruby's trunk branch with this commit to webrick.

You can edit this particular webrick file similarly in your setup. The approximate location can be found by:

gem which webrick

To actually edit the file:

nano \`ruby -e"print %x{gem which webrick}.chomp %Q{.rb\n}"\`/httpresponse.rb

(Or instead of nano, use your favorite editor.)

Solution 7 - Ruby on-Rails

JRuby version: If you're using .rvm, do this to fix it...

As mentioned by João Soares and Kjellski, this is what you can do if you want to get rid of this warning on development and you are using JRuby.

  1. Use your favorite editor to open this file:

     ~/.rvm/rubies/jruby-<version>/lib/ruby/<1.8 or 1.9>/webrick/httpresponse.rb
    
  2. Go to the line that contains this (for me it was line 205):

     if chunked? || @header['content-length']
    
  3. Change it, taken from this patch, to this:

     if chunked? || @header['content-length'] || @status == 304 || @status == 204
    
  4. Save the file and eventually restart your rails server.

Solution 8 - Ruby on-Rails

Another workaround that removes the offending line from webrick. It's just not that useful:

cd `which ruby`/../../lib/ruby/1.9.1/webrick/ && sed -i '.bak' -e'/logger.warn/d' httpresponse.rb

(you may need to sudo)

Solution 9 - Ruby on-Rails

Add

config.middleware.use Rack::ContentLength

to your application.rb file, and the warning will disappear even with webrick. This will also set Content-Length properly in production when rendering a json or text response.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionNate BirdView Question on Stackoverflow
Solution 1 - Ruby on-RailsLuis LavenaView Answer on Stackoverflow
Solution 2 - Ruby on-RailsjasoaresView Answer on Stackoverflow
Solution 3 - Ruby on-RailsootoovakView Answer on Stackoverflow
Solution 4 - Ruby on-RailsCam SongView Answer on Stackoverflow
Solution 5 - Ruby on-RailsKjellskiView Answer on Stackoverflow
Solution 6 - Ruby on-RailsMarkDBlackwellView Answer on Stackoverflow
Solution 7 - Ruby on-RailsCrimboView Answer on Stackoverflow
Solution 8 - Ruby on-RailsXavier ShayView Answer on Stackoverflow
Solution 9 - Ruby on-RailsMichael FranzlView Answer on Stackoverflow