# Getting HTTP response code from a controller

**URL:** <https://www.ruby-forum.com/t/getting-http-response-code-from-a-controller/89543>\
**Category:** Rails\
**Created:** [March 21, 2007, 2:29am UTC](https://www.ruby-forum.com/t/getting-http-response-code-from-a-controller/89543 "2007-03-21T02:29:51Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![mark](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/e9c0ed/32.png) [@mark](https://www.ruby-forum.com/u/mark)\
**Post date:** [March 21, 2007, 2:29am UTC](https://www.ruby-forum.com/t/getting-http-response-code-from-a-controller/89543/1 "2007-03-21T02:29:51Z")

</div>

Hi there, how would I get the HTTP response code (200 OK, 500, etc…)  
from a controller after a request have been ‘served’?

Thanks

---

<div class="post-metadata">

**Author:** ![mark](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/e9c0ed/32.png) [@mark](https://www.ruby-forum.com/u/mark)\
**Post date:** [March 21, 2007, 4:01am UTC](https://www.ruby-forum.com/t/getting-http-response-code-from-a-controller/89543/2 "2007-03-21T04:01:38Z")

</div>

I was able to get the response status after\_filter by doing  
response.headers[“Status”], but it _only_ displays the 200 and 302  
codes. Any ideas how I get get the others (404, 500, etc…) as well?

Thanks

Mark wrote:

> Hi there, how would I get the HTTP response code (200 OK, 500, etc…)  
> from a controller after a request have been ‘served’?
> 
> Thanks

---

<div class="post-metadata">

**Author:** ![mark](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/e9c0ed/32.png) [@mark](https://www.ruby-forum.com/u/mark)\
**Post date:** [March 21, 2007, 5:15am UTC](https://www.ruby-forum.com/t/getting-http-response-code-from-a-controller/89543/3 "2007-03-21T05:15:24Z")

</div>

You’ll have to hook it somewhere else (eg, in Mongrel or a monkey/  
mixin patch to ActionController::Base). Your controller won’t be  
consulted in the case of a 404 or 500 error. Those are usually  
generated from exceptions.

Is there a reason you need to know about these status codes in your  
controller?

---

<div class="post-metadata">

**Author:** ![mark](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/e9c0ed/32.png) [@mark](https://www.ruby-forum.com/u/mark)\
**Post date:** [March 21, 2007, 5:36am UTC](https://www.ruby-forum.com/t/getting-http-response-code-from-a-controller/89543/4 "2007-03-21T05:36:30Z")

</div>

Eden Li,

I see what you mean. The reason why I need those status codes is for a  
personal project that I’m doing and I need to store those codes in the  
DB as-they-happen.

Could you give me a starting point for the monkey/mixin patch?

Thanks

Eden Li wrote:

> You’ll have to hook it somewhere else (eg, in Mongrel or a monkey/  
> mixin patch to ActionController::Base). Your controller won’t be  
> consulted in the case of a 404 or 500 error. Those are usually  
> generated from exceptions.
> 
> Is there a reason you need to know about these status codes in your  
> controller?

---

<div class="post-metadata">

**Author:** ![mark](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/e9c0ed/32.png) [@mark](https://www.ruby-forum.com/u/mark)\
**Post date:** [March 21, 2007, 5:43am UTC](https://www.ruby-forum.com/t/getting-http-response-code-from-a-controller/89543/5 "2007-03-21T05:43:26Z")

</div>

Cool, I could have a use for that as well. Let me know if you guys  
figure it out. In the meantime I’ll try to do it too.

–  
Thiago J.  
acts\_as\_solr =\> [http://acts-as-solr.rubyforge.org](http://acts-as-solr.rubyforge.org)  
Sitealizer Web Stats =\> [http://sitealizer.rubyforge.org](http://sitealizer.rubyforge.org)

---

<div class="post-metadata">

**Author:** ![mark](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/e9c0ed/32.png) [@mark](https://www.ruby-forum.com/u/mark)\
**Post date:** [March 21, 2007, 8:34am UTC](https://www.ruby-forum.com/t/getting-http-response-code-from-a-controller/89543/6 "2007-03-21T08:34:23Z")

</div>

Here’s a first stab…

# lib/final\_response.rb

module FinalResponse  
def self.included(base)  
base.class\_eval do  
alias\_method :process\_without\_final\_response, :process  
alias\_method :process, :process\_with\_final\_response  
end  
end

def process\_with\_final\_response(\*args)  
process\_without\_final\_response(\*args)  
ensure  
final\_response if respond\_to?(:final\_response)  
end  
end

# app/controllers/my\_controller.rb

class MyController \< ActionController::Base  
include FinalResponse  
def final\_response  
logger.info(“status = #{headers[‘Status’]}”)  
end

def action  
raise  
end  
end

Your final\_response method won’t have access to the session, but  
you’ll still be OK to access models.

There’s probably a better way to do this, but this should work…
