This project is archived and is in readonly mode.

#1223 ✓stale
Elliot Winkler

ActiveResource 'head' request sends headers with a nil key

Reported by Elliot Winkler | October 16th, 2008 @ 12:53 AM | in 3.x

I've noticed that ActiveResource.exists? doesn't seem to be working correctly. Here's the backtrace I'm getting:

NoMethodError: You have a nil object when you didn't expect it!
The error occurred while evaluating nil.downcase
 from /usr/local/lib/ruby/1.8/net/http.rb:1261:in `key?'
 from /usr/local/lib/ruby/1.8/net/http.rb:1150:in `initialize_http_header'
 from /usr/local/lib/ruby/1.8/net/http.rb:1149:in `each'
 from /usr/local/lib/ruby/1.8/net/http.rb:1149:in `initialize_http_header'
 from /usr/local/lib/ruby/1.8/net/http.rb:1481:in `initialize'
 from /usr/local/lib/ruby/1.8/net/http.rb:1597:in `initialize'
 from /usr/local/lib/ruby/1.8/net/http.rb:810:in `new'
 from /usr/local/lib/ruby/1.8/net/http.rb:810:in `head'
 from /Users/elliot/Sites/localhost/htdocs/proj/work/store/vendor/rails/activeresource/lib/active_resource/connection.rb:152:in `send'
 from /Users/elliot/Sites/localhost/htdocs/proj/work/store/vendor/rails/activeresource/lib/active_resource/connection.rb:152:in `request'
 from /Users/elliot/Sites/localhost/htdocs/proj/work/store/vendor/rails/activesupport/lib/active_support/core_ext/benchmark.rb:8:in `realtime'
 from /Users/elliot/Sites/localhost/htdocs/proj/work/store/vendor/rails/activeresource/lib/active_resource/connection.rb:152:in `request'
 from /Users/elliot/Sites/localhost/htdocs/proj/work/store/vendor/rails/activeresource/lib/active_resource/connection.rb:140:in `head'
 from /Users/elliot/Sites/localhost/htdocs/proj/work/store/vendor/rails/activeresource/lib/active_resource/base.rb:554:in `exists?'

I believe this happens because when ARes does a HEAD request, it's sending a headers hash that contains a key which is nil (because it's trying to send a format header along but there's no header defined for HEAD requests in ARes::Connection::HTTP_FORMAT_HEADER_NAMES). I've attached a failing test case that pinpoints this behavior. Is this normal?

Comments and changes to this ticket

  • Elliot Winkler

    Elliot Winkler October 16th, 2008 @ 02:54 PM

    • Tag changed from activeresource to activeresource, patch

    Here's a patch that fixes the issue for me.

  • FiXato

    FiXato November 17th, 2008 @ 10:49 AM

    In revision 16b9a554db7e1bf3f5f224cdc5b4d27480e053ff correct http headers have been added per http method type, but the header for the HEAD request was ommitted. This could also be fixed by making head sent a :head method:

  • Elliot Winkler

    Elliot Winkler November 18th, 2008 @ 04:26 AM

    That actually looks better, mine was kind of a hack. I guess you should add tests?

  • Pratik

    Pratik March 7th, 2009 @ 03:09 PM

    • Assigned user set to “Pratik”
    • State changed from “new” to “incomplete”

    Looks good. But missing tests. Will apply once we have a patch with tests.


  • Joshua Bates

    Joshua Bates March 21st, 2009 @ 06:42 AM

    Here's a patch with tests. Using a Net::HTTP object in the test seems brittle, but I'm not sure there are any better choices outside of leaving it uncovered.

  • dirkbj

    dirkbj June 19th, 2009 @ 03:15 AM

    In a REST environment, HEAD is most closely associated with a GET request, but the request does not return content in the response body. Is putting :head => 'Content-Type' in the HTTP_FORMAT_HEADER_NAMES hash really the best option when :head is not sending any content as a PUT or POST would? I am of the opinion the first patch is more in the spirit of REST.

  • Pratik

    Pratik June 19th, 2009 @ 08:33 AM

    • State changed from “incomplete” to “open”
  • Jeremy Kemper

    Jeremy Kemper May 4th, 2010 @ 06:48 PM

    • Milestone changed from 2.x to 3.x
  • Santiago Pastorino

    Santiago Pastorino February 2nd, 2011 @ 05:04 PM

    • Importance changed from “” to “”

    This issue has been automatically marked as stale because it has not been commented on for at least three months.

    The resources of the Rails core team are limited, and so we are asking for your help. If you can still reproduce this error on the 3-0-stable branch or on master, please reply with all of the information you have about it and add "[state:open]" to your comment. This will reopen the ticket for review. Likewise, if you feel that this is a very important feature for Rails to include, please reply with your explanation so we can consider it.

    Thank you for all your contributions, and we hope you will understand this step to focus our efforts where they are most helpful.

  • Santiago Pastorino

    Santiago Pastorino February 2nd, 2011 @ 05:05 PM

    • State changed from “open” to “stale”

Create your profile

Help contribute to this project by taking a few moments to create your personal profile. Create your profile »

<h2 style="font-size: 14px">Tickets have moved to Github</h2>

The new ticket tracker is available at <a href=""></a>

Referenced by