I need to restrict respond_to
to JSON only, because I am constructing a web service that will provide JSON formatted output only. How do I do this?
views:
17answers:
1
+1
A:
You could create a before_filter
, defined in the ApplicationController
which would check the request.format
for JSON and then respond with an appropriate 4xx HTTP error code and message if not matched.
If to enforce this for all controllers the invoke the filter in the ApplicationController
, otherwise invoke it in the selected controllers of interest.
bjg
2010-07-20 20:42:40
Thanks for the quick answer bjg (you answered my question again!!)Which status code is more acceptable: 406 / 415?406: Content-type requested in the `accept` header is not supported,or415: Server cannot interpret the content part of the HTTP request?
rmk
2010-07-20 20:55:04
The choice of 4xx might be another good question to ask. I really am not sure what is appropriate. I don't know whether this is something that is considered application context specific or whether there are recognized conventions.
bjg
2010-07-20 21:05:55
right. I'll post this as a separate question then... thanks!
rmk
2010-07-20 21:19:36
Related question about status codes:http://stackoverflow.com/questions/3294483/http-406-and-415-error-codes
rmk
2010-07-20 21:25:34