Opened 7 years ago

Last modified 7 years ago

#29276 closed Bug

Turn the headers of HTTPResponse from str to bytes automatically — at Version 3

Reported by: sanjiely Owned by: nobody
Component: HTTP handling Version: 2.0
Severity: Normal Keywords: HTTPResponse header str bytes
Cc: Triage Stage: Unreviewed
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description (last modified by Claude Paroz)

Python: 3.6.4
Django: 2.0.3

When I return a HttpResponse object, Django library will do the job for me: turn my content from str to bytes silently.
But Django will not do the same work to the headers of HTTPResponse.

That is to say, the sample as below in the official document is not precisely correct.
(Page: https://docs.djangoproject.com/en/2.0/ref/request-response/)

>>> response = HttpResponse(my_data, content_type='application/vnd.ms-excel')
>>> response['Content-Disposition'] = 'attachment; filename="foo.xls"'

The precise usage should be:

>>> response['Content-Disposition'] = 'attachment; filename="foo.xls"'.encode('utf-8')

Otherwise, everthing will out of control If I have a Chinse character in the filename.
Wrong:

>>> response['Content-Disposition'] = 'attachment; filename="中中中中中foo.xls"'

Correct:

>>> response['Content-Disposition'] = 'attachment; filename=中中中中中foo.xls'.encode('utf-8')

Can we accept this ticket and do a minor improvement? Then we would happily only handle str in Django other than str and bytes together.
Thanks.

Yong Li

Change History (3)

comment:1 by sanjiely, 7 years ago

Summary: Advise to turn the headers of HTTPResponse from str to bytes automaticallySuggest to turn the headers of HTTPResponse from str to bytes automatically

comment:2 by sanjiely, 7 years ago

Summary: Suggest to turn the headers of HTTPResponse from str to bytes automaticallyTurn the headers of HTTPResponse from str to bytes automatically

comment:3 by Claude Paroz, 7 years ago

Description: modified (diff)
Note: See TracTickets for help on using tickets.
Back to Top