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?
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.
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.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With