如何在django中获取原始请求标头?

时间:2021-05-23 11:44:14

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?

如何在django中获取原始请求标头?我知道HttpRequest.META字典,这不是我想要的,我只想将原始标题作为字符串。有没有办法得到它?

1 个解决方案

#1


2  

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.

AFAIK,从现有的django版本(标记为<= 1.2.5)开始,没有办法从请求对象获取原始HTTP头。

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:

但是,查看django.http.HttpRequests的dev trunk(R15523)中的源代码,请求对象的基类公开了一个类似文件的接口,这表明可以使用以下内容获取原始标头:

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.

我从来没有尝试过这种方式,之前从未见过这样做,很可能,它可能还有更多。希望这能指出你正确的方向。

#1


2  

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.

AFAIK,从现有的django版本(标记为<= 1.2.5)开始,没有办法从请求对象获取原始HTTP头。

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:

但是,查看django.http.HttpRequests的dev trunk(R15523)中的源代码,请求对象的基类公开了一个类似文件的接口,这表明可以使用以下内容获取原始标头:

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.

我从来没有尝试过这种方式,之前从未见过这样做,很可能,它可能还有更多。希望这能指出你正确的方向。