#24598: JsonResponse loses encoding support in Content-Type header
-------------------------------+--------------------------------------
Reporter: funkybob | Owner: nobody
Type: Bug | Status: new
Component: HTTP handling | Version: 1.8
Severity: Normal | Resolution:
Keywords: | Triage Stage: Unreviewed
Has patch: 1 | Needs documentation: 1
Needs tests: 1 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------+--------------------------------------
Description changed by timgraham:
Old description:
> By default when a content_type is not passed to HttpResponse it will
> create one from settings.DEFAULT_CONTENT_TYPE, and attach the encoding.
>
> So, when unspecified, the header will appear as:
>
> {{{
> Content-Type: text/html; encoding=utf-8
> }}}
>
> However, if you specify the content_type, this action is no taken.
>
> That's understandable in a "consenting adults" context, however the
> JsonResponse sets content_type using kwargs.setdefault, thus forcing the
> loss of encoding annotation in all responses.
>
> I propose instead that HttpResponseBase check for a
> ``default_content_type`` property to override
> settings.DEFAULT_CONTENT_TYPE, thus:
>
> {{{
> diff --git a/django/http/response.py b/django/http/response.py
> index c8d6930..40186b5 100644
> --- a/django/http/response.py
> +++ b/django/http/response.py
> @@ -54,8 +54,8 @@ class HttpResponseBase(six.Iterator):
> self._reason_phrase = reason
> self._charset = charset
> if content_type is None:
> - content_type = '%s; charset=%s' %
> (settings.DEFAULT_CONTENT_TYPE,
> - self.charset)
> + content_type = getattr(self, 'default_content_type',
> settings.DEFAULT_CONTENT_TYPE)
> + content_type = '%s; charset=%s' % (content_type,
> self.charset)
> self['Content-Type'] = content_type
> }}}
New description:
By default when a content_type is not passed to `HttpResponse` it will
create one from `settings.DEFAULT_CONTENT_TYPE`, and attach the encoding.
So, when unspecified, the header will appear as:
{{{
Content-Type: text/html; encoding=utf-8
}}}
However, if you specify the `content_type`, this action is not taken.
That's understandable in a "consenting adults" context, however the
`JsonResponse` sets `content_type` using `kwargs.setdefault`, thus forcing
the loss of encoding annotation in all responses.
I propose instead that `HttpResponseBase` check for a
`default_content_type` property to override
`settings.DEFAULT_CONTENT_TYPE`, thus:
{{{
#!diff
diff --git a/django/http/response.py b/django/http/response.py
index c8d6930..40186b5 100644
--- a/django/http/response.py
+++ b/django/http/response.py
@@ -54,8 +54,8 @@ class HttpResponseBase(six.Iterator):
self._reason_phrase = reason
self._charset = charset
if content_type is None:
- content_type = '%s; charset=%s' %
(settings.DEFAULT_CONTENT_TYPE,
- self.charset)
+ content_type = getattr(self, 'default_content_type',
settings.DEFAULT_CONTENT_TYPE)
+ content_type = '%s; charset=%s' % (content_type,
self.charset)
self['Content-Type'] = content_type
}}}
--
--
Ticket URL: <https://code.djangoproject.com/ticket/24598#comment:3>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.
--
You received this message because you are subscribed to the Google Groups
"Django updates" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
To post to this group, send email to [email protected].
To view this discussion on the web visit
https://groups.google.com/d/msgid/django-updates/066.106b9fc1388fd1d5dfae506652a0501c%40djangoproject.com.
For more options, visit https://groups.google.com/d/optout.