On Jul 3, 4:55 am, Andrew Fong <[email protected]> wrote:
> How exactly should I handle a HEAD request in Django?
>
> So ... assuming my view looks like this...
>
> def handle_request(request):
> if request.method == 'POST': return do_post(request)
> elif request.method == 'GET': return do_get(request)
> elif request.method == 'HEAD': return do_head(request)
>
> ... what should the do_head method return? The W3 standard says "The
> HEAD method is identical to GET except that the server MUST NOT return
> a message-body in the response".
>
> How exactly do I not return a message body though? Do I just return
> None? Do I return the response I would for a GET and then modify it
> somehow?
You generally don't need to do anything different, the standard says
'the ***server***'.
So, for sane web servers, the underlying server will deal with this
and the application doesn't need to. You should just do exactly what
you would normally do for a GET request. The web server would then
return all the headers but just throw the response content away and
not return it to the client.
If you do act differently, you potentially break the requirement that
response headers for the HEAD should match what would be returned for
a GET request against same resource. Thus, the suggestion of returning
an empty string could stuff up returned content length for a start.
FWIW, in Apache/mod_wsgi it will deliberately at times translate a
HEAD into a GET when it is passed into the WSGI application. This will
occur when there are Apache output filters registered that may want to
change response headers and modify the content. For example,
mod_deflate, which would compress the response. If this isn't done and
the application acted differently for a HEAD, then the data received
by Apache output filter would not be the same as for the GET and
result returned to client would be different than what it should be.
Graham
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"Django users" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/django-users?hl=en
-~----------~----~----~----~------~----~------~--~---