I need to get all the Django request headers. From what i've read, Django simply dumps everything into the request.META
variable along with a lot aof other data. What would be the best way to get all the headers that the client sent to my Django application?
我需要获得所有Django请求头。根据我的阅读,Django将所有内容转储到请求中。元变量和许多其他数据。获取客户端发送到我的Django应用程序的所有头部的最佳方法是什么?
I'm going use these to build a httplib
request.
我将使用这些来构建httplib请求。
6 个解决方案
#1
95
According to the documentation request.META
is a "standard Python dictionary containing all available HTTP headers". If you want to get all the headers you can simply iterate through the dictionary.
根据文件要求。META是一个“包含所有可用HTTP头的标准Python字典”。如果您想获得所有的头信息,您可以通过字典进行迭代。
Which part of your code to do this depends on your exact requirement. Anyplace that has access to request
should do.
执行此操作的代码的哪一部分取决于您的确切需求。任何有访问权限的地方都应该这样做。
Update
更新
I need to access it in a Middleware class but when i iterate over it, I get a lot of values apart from HTTP headers.
我需要在一个中间件类中访问它,但是当我遍历它时,除了HTTP头之外,我还得到了很多值。
From the documentation:
从文档:
With the exception of
CONTENT_LENGTH
andCONTENT_TYPE
, as given above, anyHTTP
headers in the request are converted toMETA
keys by converting all characters to uppercase, replacing any hyphens with underscores and adding anHTTP_
prefix to the name.除了CONTENT_LENGTH和CONTENT_TYPE之外,如前所述,请求中的任何HTTP头文件都通过将所有字符转换为大写、用下划线替换任何连字符并向名称添加HTTP_前缀来转换为元键。
(Emphasis added)
(重点)
To get the HTTP
headers alone, just filter by keys prefixed with HTTP_
.
要单独获得HTTP头,只需通过前缀为HTTP_的键进行过滤。
Update 2
更新2
could you show me how I could build a dictionary of headers by filtering out all the keys from the request.META variable which begin with a HTTP_ and strip out the leading HTTP_ part.
您能告诉我如何通过从请求中过滤掉所有的键来构建一个头部字典吗?以HTTP_开头的元变量,去掉开头的HTTP_部分。
Sure. Here is one way to do it.
确定。这里有一个方法。
import re
regex = re.compile('^HTTP_')
dict((regex.sub('', header), value) for (header, value)
in request.META.items() if header.startswith('HTTP_'))
#2
16
This is another way to do it, very similar to Manoj Govindan's answer above:
这是另一种方法,很像上面Manoj Govindan的回答:
import re
regex_http_ = re.compile(r'^HTTP_.+$')
regex_content_type = re.compile(r'^CONTENT_TYPE$')
regex_content_length = re.compile(r'^CONTENT_LENGTH$')
request_headers = {}
for header in request.META:
if regex_http_.match(header) or regex_content_type.match(header) or regex_content_length.match(header):
request_headers[header] = request.META[header]
That will also grab the CONTENT_TYPE
and CONTENT_LENGTH
request headers, along with the HTTP_
ones. request_headers['some_key]
== request.META['some_key']
.
它还将获取CONTENT_TYPE和CONTENT_LENGTH请求头,以及HTTP_项。request_header[' some_key]= = request.META[' some_key ']。
Modify accordingly if you need to include/omit certain headers. Django lists a bunch, but not all, of them here: https://docs.djangoproject.com/en/dev/ref/request-response/#django.http.HttpRequest.META
如果需要包含/省略某些头部,则相应地进行修改。Django在这里列出了很多,但不是全部:https://docs.djangoproject.com/en/dev/ref/request-response/# django.httprequest.meta
Django's algorithm for request headers:
Django的请求头算法:
- Replace hyphen
-
with underscore_
- 用下划线_替换连字符
- Convert to UPPERCASE.
- 转换为大写。
- Prepend
HTTP_
to all headers in original request, except forCONTENT_TYPE
andCONTENT_LENGTH
. - 在原始请求中,除了CONTENT_TYPE和CONTENT_LENGTH之外,将HTTP_前置到所有头部。
The values of each header should be unmodified.
每个头的值应该不被修改。
#3
4
I don't think there is any easy way to get only HTTP headers. You have to iterate through request.META dict to get what all you need.
我认为只有HTTP报头是不容易的。您必须遍历请求。获取你所需要的一切。
django-debug-toolbar takes the same approach to show header information. Have a look at this file responsible for retrieving header information.
django-debug-toolbar使用相同的方法显示标题信息。查看负责检索头信息的文件。
#4
3
request.META.get('HTTP_AUTHORIZATION') /python3.6/site-packages/rest_framework/authentication.py
request.META.get(HTTP_AUTHORIZATION)/ python3.6 /网站/ rest_framework / authentication.py
you can get that from this file though...
你可以从这个文件中得到。
#5
0
For what it's worth, it appears your intent is to use the incoming HTTP request to form another HTTP request. Sort of like a gateway. There is an excellent module django-revproxy that accomplishes exactly this.
值得注意的是,您的意图似乎是使用传入的HTTP请求来形成另一个HTTP请求。有点像网关。有一个优秀的模块django-revproxy可以实现这一点。
The source is a pretty good reference on how to accomplish what you are trying to do.
关于如何完成您正在尝试做的事情,源代码是一个很好的参考。
#6
0
<b>request.META</b><br>
{% for k_meta, v_meta in request.META.items %}
<code>{{ k_meta }}</code> : {{ v_meta }} <br>
{% endfor %}
#1
95
According to the documentation request.META
is a "standard Python dictionary containing all available HTTP headers". If you want to get all the headers you can simply iterate through the dictionary.
根据文件要求。META是一个“包含所有可用HTTP头的标准Python字典”。如果您想获得所有的头信息,您可以通过字典进行迭代。
Which part of your code to do this depends on your exact requirement. Anyplace that has access to request
should do.
执行此操作的代码的哪一部分取决于您的确切需求。任何有访问权限的地方都应该这样做。
Update
更新
I need to access it in a Middleware class but when i iterate over it, I get a lot of values apart from HTTP headers.
我需要在一个中间件类中访问它,但是当我遍历它时,除了HTTP头之外,我还得到了很多值。
From the documentation:
从文档:
With the exception of
CONTENT_LENGTH
andCONTENT_TYPE
, as given above, anyHTTP
headers in the request are converted toMETA
keys by converting all characters to uppercase, replacing any hyphens with underscores and adding anHTTP_
prefix to the name.除了CONTENT_LENGTH和CONTENT_TYPE之外,如前所述,请求中的任何HTTP头文件都通过将所有字符转换为大写、用下划线替换任何连字符并向名称添加HTTP_前缀来转换为元键。
(Emphasis added)
(重点)
To get the HTTP
headers alone, just filter by keys prefixed with HTTP_
.
要单独获得HTTP头,只需通过前缀为HTTP_的键进行过滤。
Update 2
更新2
could you show me how I could build a dictionary of headers by filtering out all the keys from the request.META variable which begin with a HTTP_ and strip out the leading HTTP_ part.
您能告诉我如何通过从请求中过滤掉所有的键来构建一个头部字典吗?以HTTP_开头的元变量,去掉开头的HTTP_部分。
Sure. Here is one way to do it.
确定。这里有一个方法。
import re
regex = re.compile('^HTTP_')
dict((regex.sub('', header), value) for (header, value)
in request.META.items() if header.startswith('HTTP_'))
#2
16
This is another way to do it, very similar to Manoj Govindan's answer above:
这是另一种方法,很像上面Manoj Govindan的回答:
import re
regex_http_ = re.compile(r'^HTTP_.+$')
regex_content_type = re.compile(r'^CONTENT_TYPE$')
regex_content_length = re.compile(r'^CONTENT_LENGTH$')
request_headers = {}
for header in request.META:
if regex_http_.match(header) or regex_content_type.match(header) or regex_content_length.match(header):
request_headers[header] = request.META[header]
That will also grab the CONTENT_TYPE
and CONTENT_LENGTH
request headers, along with the HTTP_
ones. request_headers['some_key]
== request.META['some_key']
.
它还将获取CONTENT_TYPE和CONTENT_LENGTH请求头,以及HTTP_项。request_header[' some_key]= = request.META[' some_key ']。
Modify accordingly if you need to include/omit certain headers. Django lists a bunch, but not all, of them here: https://docs.djangoproject.com/en/dev/ref/request-response/#django.http.HttpRequest.META
如果需要包含/省略某些头部,则相应地进行修改。Django在这里列出了很多,但不是全部:https://docs.djangoproject.com/en/dev/ref/request-response/# django.httprequest.meta
Django's algorithm for request headers:
Django的请求头算法:
- Replace hyphen
-
with underscore_
- 用下划线_替换连字符
- Convert to UPPERCASE.
- 转换为大写。
- Prepend
HTTP_
to all headers in original request, except forCONTENT_TYPE
andCONTENT_LENGTH
. - 在原始请求中,除了CONTENT_TYPE和CONTENT_LENGTH之外,将HTTP_前置到所有头部。
The values of each header should be unmodified.
每个头的值应该不被修改。
#3
4
I don't think there is any easy way to get only HTTP headers. You have to iterate through request.META dict to get what all you need.
我认为只有HTTP报头是不容易的。您必须遍历请求。获取你所需要的一切。
django-debug-toolbar takes the same approach to show header information. Have a look at this file responsible for retrieving header information.
django-debug-toolbar使用相同的方法显示标题信息。查看负责检索头信息的文件。
#4
3
request.META.get('HTTP_AUTHORIZATION') /python3.6/site-packages/rest_framework/authentication.py
request.META.get(HTTP_AUTHORIZATION)/ python3.6 /网站/ rest_framework / authentication.py
you can get that from this file though...
你可以从这个文件中得到。
#5
0
For what it's worth, it appears your intent is to use the incoming HTTP request to form another HTTP request. Sort of like a gateway. There is an excellent module django-revproxy that accomplishes exactly this.
值得注意的是,您的意图似乎是使用传入的HTTP请求来形成另一个HTTP请求。有点像网关。有一个优秀的模块django-revproxy可以实现这一点。
The source is a pretty good reference on how to accomplish what you are trying to do.
关于如何完成您正在尝试做的事情,源代码是一个很好的参考。
#6
0
<b>request.META</b><br>
{% for k_meta, v_meta in request.META.items %}
<code>{{ k_meta }}</code> : {{ v_meta }} <br>
{% endfor %}