Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get raw request headers in django?

Tags:

python

django

How can I get raw request headers in django? I am aware of HttpRequest.META dictionary, this is not what I want, I just want the raw headers as string. Is there any way to get it?

like image 367
nagaru Avatar asked Feb 13 '11 06:02

nagaru


People also ask

How can I get header data in Django?

Hello @kartik, You can use request. headers to access the HTTP headers. A case insensitive, dict-like object that provides access to all HTTP-prefixed headers (plus Content-Length and Content-Type) from the request.

What is HTTP request in Django?

Django uses request and response objects to pass state through the system. When a page is requested, Django creates an HttpRequest object that contains metadata about the request. Then Django loads the appropriate view, passing the HttpRequest as the first argument to the view function.

What is request Meta get in Django?

META contains all the metadata of the HTTP request that is coming to your Django server, it can contain the user agent, ip address, content type, and so on.


1 Answers

AFAIK, as of the existing django releases (tagged <=1.2.5), there isn't a way to get at the raw HTTP headers from the request object.

However, looking at the source in the dev trunk (R15523) for django.http.HttpRequests, the base class for the request object exposes a file-like interface which would suggest that one would be able to get the raw headers using something like:

def dump_request_headers(request):
    dump = "".join(request.xreadlines())
    return HttpResponse("<pre>%s</pre>" % dump)

I have never tried this and have never seen this done before so, chances are, there may be more to it than that. Hope this points you in the right direction.

like image 113
Shawn Chin Avatar answered Oct 21 '22 00:10

Shawn Chin