# \[Rails Rev 6032\] HTTP Status Code 422 for ActiveResource Err

**URL:** <https://www.ruby-forum.com/t/rails-rev-6032-http-status-code-422-for-activeresource-err/85781>\
**Category:** Rails\
**Created:** [February 16, 2007, 9:05am UTC](https://www.ruby-forum.com/t/rails-rev-6032-http-status-code-422-for-activeresource-err/85781 "2007-02-16T09:05:02Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![aq1018](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/ea5d25/32.png) [@aq1018](https://www.ruby-forum.com/u/aq1018)\
**Post date:** [February 16, 2007, 9:05am UTC](https://www.ruby-forum.com/t/rails-rev-6032-http-status-code-422-for-activeresource-err/85781/1 "2007-02-16T09:05:02Z")

</div>

I was playing around with ActiveResource from the edge rails revision  
6032, and had a nightmare trying to get returned errors from my  
RESTful controller.

After a long search, I found out in here: [http://dev.rubyonrails.org/](http://dev.rubyonrails.org/)  
browser/trunk/activeresource/test/base\_errors\_test.rb?rev=6032  
on line 7 that you supposed to return http status code 422 along with  
your error xml!!

so suppose you have code like this:

format.xml { render :xml =\> @user.errors.to\_xml }

active resource will die horribly!!

The correct way is format.xml { render :xml =\>  
@user.errors.to\_xml, :status =\> 422 }

I was curious about what is this mysterious status code, so I searched  
in google, wikipedia, etc…

Could find it!! What the heck does 422 mean?!

Also found some change logs here documenting this:  
[http://dev.rubyonrails.org/browser/trunk/activeresource/CHANGELOG?rev=6032](http://dev.rubyonrails.org/browser/trunk/activeresource/CHANGELOG?rev=6032)

But I’m still unsatisfied about it! Did some one in rails developement  
team just came up with his own error code?

Hopefully that’s not the case…

Anyone knows what does 422 mean?

---

<div class="post-metadata">

**Author:** ![aq1018](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/ea5d25/32.png) [@aq1018](https://www.ruby-forum.com/u/aq1018)\
**Post date:** [February 16, 2007, 9:35am UTC](https://www.ruby-forum.com/t/rails-rev-6032-http-status-code-422-for-activeresource-err/85781/2 "2007-02-16T09:35:05Z")

</div>

On 2/16/07, aq1018 [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> so suppose you have code like this:
> 
> Anyone knows what does 422 mean?  
> [http://www.zvon.org/tmRFC/RFC2518/Output/chapter10.html#sub3](http://www.zvon.org/tmRFC/RFC2518/Output/chapter10.html#sub3)

The 422 (Unprocessable Entity) status code means the server  
understands the content type of the request entity (hence a  
415(Unsupported Media Type) status code is inappropriate), and the  
syntax of the request entity is correct (thus a 400 (Bad Request)  
status code is inappropriate) but was unable to process the contained  
instructions. For example, this error condition may occur if an XML  
request body contains well-formed (i.e., syntactically correct), but  
semantically erroneous XML instructions.

format.xml { render :xml =\> @user.errors.to\_xml, :status =\>  
:unprocessable\_entity }

–  
Rick O.  
[http://weblog.techno-weenie.net](http://weblog.techno-weenie.net)  
[http://mephistoblog.com](http://mephistoblog.com)

---

<div class="post-metadata">

**Author:** ![aq1018](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/ea5d25/32.png) [@aq1018](https://www.ruby-forum.com/u/aq1018)\
**Post date:** [February 16, 2007, 9:48am UTC](https://www.ruby-forum.com/t/rails-rev-6032-http-status-code-422-for-activeresource-err/85781/3 "2007-02-16T09:48:06Z")

</div>

Hi,

Here is more information about the 422 Unprocessable Entity  
status code:

[http://dev.rubyonrails.org/ticket/7097](http://dev.rubyonrails.org/ticket/7097)

Its also in the IANA HTTP Status Code registry:

[Hypertext Transfer Protocol (HTTP) Status Code Registry](http://www.iana.org/assignments/http-status-codes)

In this case WebDAV defined a status code to deal with the  
case where a request contains information that can be parsed  
but is contains invalid information.

Its considered good practice to reuse registered status codes  
(even those from RFC’s other than 2616) rather than inventing  
your own, provided they fit your needs. In this case 422 is  
a perfect fit when you want to tell the client that the data  
they submitted was parsed, but found to be invalid.

BTW: in your code I’d probably do this instead:

format.xml { render :xml =\> @user.errors.to\_xml, :status  
=\> :unprocessable\_entity }

Most people don’t remember what all the status codes mean,  
and the symbol names are a bit more descriptive and easier  
to remember.

Dan

On 16-Feb-07, at 12:04 AM, aq1018 wrote:

> Could find it!! What the heck does 422 mean?!  
> Anyone knows what does 422 mean?

* * *

Dan K.  
Autopilot Marketing Inc.

Email: removed\_email\_address@domain.invalid  
Phone: 1 (604) 820-0212  
Web: [http://autopilotmarketing.com/](http://autopilotmarketing.com/)  
vCard: [http://autopilotmarketing.com/~dan.kubb/vcard](http://autopilotmarketing.com/~dan.kubb/vcard)

* * *

---

<div class="post-metadata">

**Author:** ![aq1018](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/ea5d25/32.png) [@aq1018](https://www.ruby-forum.com/u/aq1018)\
**Post date:** [February 16, 2007, 7:39pm UTC](https://www.ruby-forum.com/t/rails-rev-6032-http-status-code-422-for-activeresource-err/85781/4 "2007-02-16T19:39:45Z")

</div>

So, wait… we’re supposed to always return 422 for a REST request that  
has errors?

Or, as the OP said:

so suppose you have code like this:

format.xml { render :xml =\> @user.errors.to\_xml }

active resource will die horribly!!

The correct way is format.xml { render :xml =\>  
@user.errors.to\_xml, :status =\> 422 }

Never mind the numeric code versus the names (I have just as hard a time  
remembering the names as the numerics, by the way)… it sounds like  
scaffold\_resource isn’t doing the full job for us. (I’ve given up on  
to\_xml too… it never gives me enough control).

b

---

<div class="post-metadata">

**Author:** ![aq1018](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/ea5d25/32.png) [@aq1018](https://www.ruby-forum.com/u/aq1018)\
**Post date:** [February 19, 2007, 8:08am UTC](https://www.ruby-forum.com/t/rails-rev-6032-http-status-code-422-for-activeresource-err/85781/5 "2007-02-19T08:08:52Z")

</div>

-----BEGIN PGP SIGNED MESSAGE-----  
Hash: SHA1

Hi Ben,

> So, wait… we’re supposed to always return 422 for a REST request  
> that  
> has errors?

The short answer to your question is: Yes, when a model is invalid  
the best status code to return is 422.

More broadly, there are many different reasons to return an error,  
422 is only useful for one specific kind. Whenever the client  
sends something in the request the server likes you can return  
any of the 4xx status codes…

It could be anything from submitting a request to a URL that  
doesn’t exist (404) or has been explicitly deleted (410). Maybe  
the URL is too long to be handled (414) or maybe the method isn’t  
allowed (405). Maybe the request can be parsed, but the data you get  
after parsing is invalid (422). There’s more in the IANA status code  
registry but you get the idea:

[Hypertext Transfer Protocol (HTTP) Status Code Registry](http://www.iana.org/assignments/http-status-codes)

Even though a well designed client will treat any 4xx status that  
it doesn’t understand as 400, I still wouldn’t use 400 unless there  
was no other 4xx status code in the registry that matches.

When you use a more specific error status code you give the client  
(browser, bot, etc) a better indication of what went wrong, improving  
the odds that it can self-recover without human intervention.

> (I’ve given up on to\_xml too… it never gives me enough control).  
> The scaffold\_resource should probably be updated to use 422, but I  
> agree: I don’t think to\_xml like the above example is too useful  
> except in the uncommon case (for me) where I only have a single  
> model object in an action.

* * *

Thanks,

Dan

* * *

Dan K.  
Autopilot Marketing Inc.

Email: removed\_email\_address@domain.invalid  
Phone: 1 (604) 820-0212  
Web: [http://autopilotmarketing.com/](http://autopilotmarketing.com/)  
vCard: [http://autopilotmarketing.com/~dan.kubb/vcard](http://autopilotmarketing.com/~dan.kubb/vcard)

* * *

-----BEGIN PGP SIGNATURE-----  
Version: GnuPG v1.4.2.2 (Darwin)

iD8DBQFF2Uzv4DfZD7OEWk0RAuUtAJ9E9XceMoPxAnrIeB3BmfM+LZ+wkACdHfeN  
skPYab+ru/Yk4jQoLWJtcYY=  
=fPhM  
-----END PGP SIGNATURE-----

---

<div class="post-metadata">

**Author:** ![aq1018](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/ea5d25/32.png) [@aq1018](https://www.ruby-forum.com/u/aq1018)\
**Post date:** [February 17, 2007, 12:12pm UTC](https://www.ruby-forum.com/t/rails-rev-6032-http-status-code-422-for-activeresource-err/85781/6 "2007-02-17T12:12:08Z")

</div>

Thanks for the information, guys, that helped me out alot.

I didn’t want to use :status =\> 422 either, it’s somewhat ugly…

Yes, I think for ActiveResource to properly hand error messages, the  
RESTful controller needs to send :status =\> :unprocessable\_entity  
whenever you use model.errors.to\_xml. Example:

def create  
@person = Person.new(params[:person])  
respond\_to do |format|  
if @person.save  
format.html { redirect\_to person\_path(@person) }  
format.xml { head :created, :location =\> person\_url(@person) }  
else  
format.html { render :action =\> ‘new’ }  
format.xml { render :xml =\> @person.errors.to\_xml :status  
=\> :unprocessable\_entity }  
end  
end

---

<div class="post-metadata">

**Author:** ![aq1018](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/ea5d25/32.png) [@aq1018](https://www.ruby-forum.com/u/aq1018)\
**Post date:** [February 17, 2007, 12:15pm UTC](https://www.ruby-forum.com/t/rails-rev-6032-http-status-code-422-for-activeresource-err/85781/7 "2007-02-17T12:15:12Z")

</div>

Errr… mistake in the code…

this is the right one… (missed the “end” )

def create  
@person = Person.new(params[:person])  
respond\_to do |format|  
if @person.save  
format.html { redirect\_to person\_path(@person) }  
format.xml { head :created, :location =\> person\_url(@person) }  
else  
format.html { render :action =\> ‘new’ }  
format.xml { render :xml =\> @person.errors.to\_xml :status  
=\> :unprocessable\_entity }  
end  
end  
end
