Repository navigation
Description
Checklist
- I have verified that that issue exists against the
masterbranch of Django REST framework. - I have searched for similar issues in both open and closed tickets and cannot find a duplicate.
- This is not a usage question. (Those should be directed to the discussion group instead.)
- This cannot be dealt with as a third party library. (We prefer new functionality to be in the form of third party libraries where possible.)
- I have reduced the issue to the simplest possible case.
- I have included a failing test as a pull request. (If you are unable to do so we can still accept the issue.)
Problem description
The default request header 'Accept' on many browsers is something like Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8. This means that currently, if the response is not text/html, then the response Content-Type incorrectly contains the q= parameter. (According to this, the q parameter specifies which content-types the browser prefers).
It looks like it is occurring because:
- DefaultContentNegotiation.select_renderer returns the
full_media_type(which includes the extraAccept"parameters" likeq) - ApiView.perform_content_negotiation returns the
renderer, full_media_typetuple - ApiView.initial sets
request.accepted_media_typeequal to thatfull_media_type - ApiView.finalize_response sets
response.accepted_media_typeequal torequest.accepted_media_type - Response.rendered_content is setting the response
Content-Typeto the value ofresponse.accepted_media_type.
This only came to light because I'm running nginx in front of django, and it was not recognizing the content-type (as application/json is different from application/json;q=0.9). As far as I can tell, this is actually an invalid Content-Type (https://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html#sec14.17).
Steps to reproduce
curl --verbose http://example.com/api/model?format=json --H 'Accept: text/html,application/xhtml+xml,application/xml;q=0.9,/;q=0.8' > /dev/null
Expected behavior
Content-Type: application/json; charset=utf-8
Actual behavior
Content-Type: application/json;q=0.9; charset=utf-8
Activity
lovelydinosaur commentedon Jul 11, 2016
Closing this as I couldn't replicate the described behavior...
--> 406 {"detail":"Could not satisfy the request Accept header."}
Or this, without the
format=json...More than happy to reopen this if you can demo an example against
http://restframework.herokuapp.com/that I can replicate, or if you can demo that the behavior has changed, and that the latest version of REST framework running the tutorial does demonstrate the issue, or if you can provide an example pull request that demonstrates that we do have an issue.lsowen commentedon Jul 11, 2016
They are different.
In
master, the return value is thefull_media_type(e.g. includesqandindent).In
3.1, the return value isrenderer.media_type, which does not include the extra parameters passed in by theAcceptheader.lovelydinosaur commentedon Jul 11, 2016
Thanks for confirming.
lovelydinosaur commentedon Jul 11, 2016
Now resolved, thanks again @lsowen!
lsowen commentedon Jul 11, 2016
@tomchristie I appreciate the quick response. Thanks for your help!
lovelydinosaur commentedon Jul 11, 2016
Most welcome