Navigation Menu

Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Default URI format is forced over Accept header #19514

Closed
gmile opened this issue Mar 25, 2015 · 7 comments
Closed

Default URI format is forced over Accept header #19514

gmile opened this issue Mar 25, 2015 · 7 comments

Comments

@gmile
Copy link
Contributor

gmile commented Mar 25, 2015

Rails forces format, specified in default over the one, provided in Accept header.

This is somewhat counterintuitive. Semantic meaning of default is to yield a fallback option if no other option was provided. However, right now default is forced.

Upon digging up the Rails, codebase, it became apparent that Rails takes the format param as the one having priority over Accept header.

I personally don't think it is correct, but I can't quickly establish anything RFC-like to prove which one should take precedence: the inferred (in my case: default) response content type, or the one provided by the Accept header.

I'm going to provide an example app (via a template) to back my point.

@kostyl
Copy link

kostyl commented Mar 25, 2015

@gmile will it change respond_with behavior?

@gmile
Copy link
Contributor Author

gmile commented Mar 25, 2015

@kostyl hmm, no, I don't think anything should break. Things to consider:

  1. you must make sure you always pass in .json type with your URLs (do you?),
  2. I think browser (actually, some ember middleware) should set Accept: application/json automatically, but I'm not certain,
  3. tests pass, so I think we're good at least in some way?

@matthewd
Copy link
Member

The :format parameter is, by design, a means of overriding the best guess that would be derived from Accept.

So, setting it as a route's default parameter is not something I'd expect to need a lot.

.. what behaviour were you intending to achieve?

@gmile
Copy link
Contributor Author

gmile commented Mar 25, 2015

@matthewd I 100% agree with you.

I'd expect the following:

  1. if Accept: ... header is given – take it on a priority,
  2. if :format is given – take it,
  3. if :format is not given – go with default format (if specified),
  4. if none of 1–3 worked, return 415 error.

Meanwhile, in our app we've removed defaults: { format: :json } to work over this issue.

@matthewd
Copy link
Member

Yeah, that's not going to happen: browsers (for example) always send Accept; the above ordering would entirely break :format.

@gmile
Copy link
Contributor Author

gmile commented Mar 25, 2015

@matthewd hmm indeed, now I fully understand your point. Thank you!

@KES777
Copy link

KES777 commented Feb 24, 2018

Actually I came here with the question: Should server relay on Accept header or requested format in URL?

Unfortunately I did not find any RFC or specs that define this precedence explicitly.

My personal opinion is:

Client software may support many different formats and will send all of them in Accept header, but user of this software may prefer different format with lower priority. So typing format in URI he prioritize desired format. Without this client software (browser/user agent) always will abandon user's wish.

So priority should be next:

  1. Take format from query. This will give a chance to scripts chose right format:
    /script.pl?format=html or /script.pl?format.json
  2. If no format in query then determine format from path: /script.pl
  3. If no format determined yet then start content negotiation relaying on Accept- headers

May someone provide proof link for this?

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

No branches or pull requests

4 participants