Hello,
I am having trouble with content negotiation due to some Accept headers not clearly specifying an order amongst all desired media types. For example, Safari provides:
application/xml,application/xhtml+xml,text/html;q=0.9,text/plain;q=0.8,image/png,*/*;q=0.5
Right now, if a Rails resource can be represented both in html and application/xml, rails will return an application/xml representation.
Asking about it in the rest-discuss mailing list, the http specs do not provide any guide on which media-type to pick if multiple ones have the same q value. In Safari's example, there are three possible media types that can be picked.
Other language and frameworks have decided to use a first-q value, second-alphabetical sorting of the list and find the first one that it is capable of providing. This solves the problem for typical representations because json and xml comes after html.
I am attaching my original question on the rest-discuss list and mike's answer which mentions the first framework (his link shows other - python and erlang - frameworks that chose the same path).
The rails code that affects it is within the mime_responds.rb:
if ActionController::Base.use_accept_header @mime_type_priority = Array(Mime::Type.lookup_by_extension (@request.parameters[:format]) || @request.accepts) else @mime_type_priority = [@request.format] end
The issue with Rails by default is that the first priority for Safari in the list is priority = 'application/xml' and @responses do not include it (Rails registered xml only, not application/xml): if @responses[priority] @responses[priority].call return # mime type match found, be happy and return end
If we register a application/xml, then Safari starts getting a xml representation, where most would give back a html representation back.
Any suggestions?
note: the sorting is according to the name after the /
Regards
Guilherme Silveira Caelum | Ensino e Inovação http://www.caelum.com.br/