"The Content-Length and Content-Type headers are automatically set when
they can be guessed from contents of open_file".
But code inside FileResponse object does not use contents of open_file to
guess content type.
It uses "mimetypes.guess_type()" method to determine content type.
"mimetypes.guess_type()" method uses file extension to to determine
content type.
So a file response object such as this will set the content type to
"text/html".
{{{
FileResponse(open('test.gif', 'rb'), filename="test.html")
}}}
Documentation should correctly mention how FileResponse object determine
content type.
--
Ticket URL: <https://code.djangoproject.com/ticket/34404>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.
* cc: chamalsl (added)
--
Ticket URL: <https://code.djangoproject.com/ticket/34404#comment:1>
* type: Uncategorized => Cleanup/optimization
* stage: Unreviewed => Accepted
Old description:
> Below is an excerpt from the Django document for FileResponse object.
> Link: [https://docs.djangoproject.com/en/4.1/ref/request-response/]
>
> "The Content-Length and Content-Type headers are automatically set when
> they can be guessed from contents of open_file".
>
> But code inside FileResponse object does not use contents of open_file to
> guess content type.
> It uses "mimetypes.guess_type()" method to determine content type.
> "mimetypes.guess_type()" method uses file extension to to determine
> content type.
>
> So a file response object such as this will set the content type to
> "text/html".
>
> {{{
> FileResponse(open('test.gif', 'rb'), filename="test.html")
> }}}
>
> Documentation should correctly mention how FileResponse object determine
> content type.
New description:
Below is an excerpt from the Django document for FileResponse object.
Link: [https://docs.djangoproject.com/en/4.1/ref/request-response/]
"The Content-Length and Content-Type headers are automatically set when
they can be guessed from contents of open_file".
But code inside FileResponse object does not use contents of open_file to
guess content type.
It uses "mimetypes.guess_type()" method to determine content type.
"mimetypes.guess_type()" method uses file extension to to determine
content type.
So a file response object such as this will set the content type to
"text/html".
{{{
FileResponse(open('test.gif', 'rb'), filename="test.html")
}}}
Documentation should correctly mention how FileResponse object determine
content type.
--
Comment:
Thanks for the report.
--
Ticket URL: <https://code.djangoproject.com/ticket/34404#comment:2>
* owner: nobody => Ayush Bisht
* status: new => assigned
--
Ticket URL: <https://code.djangoproject.com/ticket/34404#comment:3>
* needs_better_patch: 0 => 1
* has_patch: 0 => 1
* easy: 0 => 1
Comment:
[https://github.com/django/django/pull/16644 PR]
--
Ticket URL: <https://code.djangoproject.com/ticket/34404#comment:4>
* needs_better_patch: 1 => 0
* stage: Accepted => Ready for checkin
--
Ticket URL: <https://code.djangoproject.com/ticket/34404#comment:5>
* status: assigned => closed
* resolution: => fixed
Comment:
In [changeset:"fc266b694b9129e44b597d8a56927ee13fdac358" fc266b69]:
{{{
#!CommitTicketReference repository=""
revision="fc266b694b9129e44b597d8a56927ee13fdac358"
Fixed #34404 -- Clarified how FileResponse set Content-Type header.
}}}
--
Ticket URL: <https://code.djangoproject.com/ticket/34404#comment:6>
Comment (by Mariusz Felisiak <felisiak.mariusz@…>):
In [changeset:"eff2ba3f8d2f172c64b9df08db8d188e11bae855" eff2ba3f]:
{{{
#!CommitTicketReference repository=""
revision="eff2ba3f8d2f172c64b9df08db8d188e11bae855"
[4.2.x] Fixed #34404 -- Clarified how FileResponse set Content-Type
header.
Backport of fc266b694b9129e44b597d8a56927ee13fdac358 from main
}}}
--
Ticket URL: <https://code.djangoproject.com/ticket/34404#comment:7>