Skip to content

Fallback to default_content if wildcard present in Accept header#21

Open
francois wants to merge 1 commit into
cehoffman:masterfrom
francois:return-wildcard-when-asked-to-do-so
Open

Fallback to default_content if wildcard present in Accept header#21
francois wants to merge 1 commit into
cehoffman:masterfrom
francois:return-wildcard-when-asked-to-do-so

Conversation

@francois
Copy link
Copy Markdown

@francois francois commented May 9, 2013

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

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.
@francois
Copy link
Copy Markdown
Author

francois commented May 9, 2013

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant