I am using rails 3.2.8 on Ubuntu 12.04
I am building a web service with json only support. Now if someone sends me data that is not json but sets content-type to application/json the rails application raises an exception and returns 500 (internal server error).
This exception happens within rails before the controller action is called as rails is trying to create the params structure and parse the incoming data as JSON.
I am not sure this is right behaviour. I would have prefered if it rescued the exception and let me know in some special handler or within my action with some variable set. I would want to indicate 415 (unsupported media type) or 422 (unprocessable entity). My guess is 422 is more appropriate in this case.
Any ideas on how this can be accomplished?
Guess you need to change middleware a bit. Either create custom middleware and insert it before
ParamsParser, or override theParamsParser.Take a look at this request, there is a sample of
ParamsParseroverriding.Creating custom middlevare has another advantage: if data is
jsonbut content-type is notapplication/json, you can fix it here (example).