views:

17

answers:

1

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?

+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
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
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
right. I'll post this as a separate question then... thanks!
rmk
Related question about status codes:http://stackoverflow.com/questions/3294483/http-406-and-415-error-codes
rmk