Had a painful experience last night trying to get Rails to properly handle a JSON request coming from an Objective-C client. Rails was issuing a semi-exotic HTTP error 406. Error 406 means that the user agent – your browser, my iPhone app – cannot handle the response being sent to it. Apparently it all stems from an HTTP request header, Accept, which tells the server what content it can 'digest'.
In any case, to cure this problem I looked at what Accept header Firefox was sending when Rails was successfully handling its requests. By copying Firefox's Accept header and sending it to with my request, I was able to overcome the 406 error and get a happy 200 response.
The Accept string that worked was:
text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
If this does not work, also examine the Rails controller. Make sure that all REST methods return JSON representations.
One reply on “Rails HTTP error 406 in response to JSON requests”
I have this problem but with a JSONP request.
$.getJSON('http://remotesite.com/get_data.json&callback=?',function(jsonp){ alert("Yeah") });
Thanks