sinatra-respond_to icon indicating copy to clipboard operation
sinatra-respond_to copied to clipboard

Fallback to default_content if wildcard present in Accept header

Open francois opened this issue 12 years ago • 1 comments

If a browser makes a request like this:

Accept: text/csv,*.*

And the application is implemented like this:

set :default_content, :html

get "/" do
  respond_to do |wants|
    format.html { erb :home }
  end
end

The request would return a 404, even though the caller specifically said it was ready to accept any mime type.

I found this when building an API that does not return HTML: it returns JSON or CSV, but browsers request HTML, XHTML and /, in this order. By catering to what the caller asked for, the API is more easily explorable in a browser.


This change is Reviewable

francois avatar May 09 '13 22:05 francois

This is actually incomplete. When the Accept header's first mime type is unknown, then we return the default, rather than the most appropriate type. For example:

Accept: text/xml,text/csv,*.*

And a respond_to block of:

set :default_content, :html

get "/" do
  respond_to do |wants|
    wants.html { ... }
    wants.csv { ... }
  end
end

The response will be HTML, and not CSV. In this case, it would be more appropriate to have CSV, since that's how the caller ordered the values. I'll work on this some more.

francois avatar May 09 '13 23:05 francois