如何获取 Django 中的所有请求标头?
我需要获取所有 Django 请求标头。据我所知,Django 只是将所有内容以及许多其他数据转储到 request.META 变量中。获取客户端发送到我的 Django 应用程序的所有标头的最佳方法是什么?
我将使用它们来构建 httplib
请求。
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 of other data. What would be the best way to get all the headers that the client sent to my Django application?
I'm going use these to build a httplib
request.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(10)
根据 文档
request.META
是一个“包含所有可用 HTTP 标头的标准 Python 字典”。如果您想获取所有标题,您只需迭代字典即可。代码的哪一部分执行此操作取决于您的具体要求。任何有权访问
request
的地方都可以。更新
从文档中:
(强调)
要单独获取
HTTP
标头,只需按前缀为HTTP_
的键进行过滤即可。更新2
当然。这是一种方法。
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.Which part of your code to do this depends on your exact requirement. Anyplace that has access to
request
should do.Update
From the documentation:
(Emphasis added)
To get the
HTTP
headers alone, just filter by keys prefixed withHTTP_
.Update 2
Sure. Here is one way to do it.
从 Django 2.2 开始,您可以使用 request.headers 来访问 HTTP 标头。来自有关 HttpRequest.headers 的文档:
要获取所有标头,您可以使用
request.headers.keys()
或request.headers.items()
。Starting from Django 2.2, you can use
request.headers
to access the HTTP headers. From the documentation on HttpRequest.headers:To get all headers, you can use
request.headers.keys()
orrequest.headers.items()
.这是另一种方法,与上面 Manoj Govindan 的答案非常相似:
这也会获取
CONTENT_TYPE
和CONTENT_LENGTH
请求标头,以及HTTP_
。request_headers['some_key]
==request.META['some_key']
。如果您需要包含/省略某些标头,请进行相应修改。 Django 在这里列出了一堆,但不是全部: https: //docs.djangoproject.com/en/dev/ref/request-response/#django.http.HttpRequest.META
Django 的请求头算法:
-
替换为下划线_
HTTP_
添加到原始请求中的所有标头(CONTENT_TYPE
和CONTENT_LENGTH
除外)。每个标头的值不应被修改。
This is another way to do it, very similar to Manoj Govindan's answer above:
That will also grab the
CONTENT_TYPE
andCONTENT_LENGTH
request headers, along with theHTTP_
ones.request_headers['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's algorithm for request headers:
-
with underscore_
HTTP_
to all headers in original request, except forCONTENT_TYPE
andCONTENT_LENGTH
.The values of each header should be unmodified.
只需使用 HttpRequest.headers 从 Django 2.2 开始。以下示例直接取自 Django 文档 下的 请求和响应对象部分。
Simply you can use HttpRequest.headers from Django 2.2 onward. Following example is directly taken from the official Django Documentation under Request and response objects section.
request.META.get('HTTP_AUTHORIZATION')
/python3.6/site-packages/rest_framework/authentication.py
你可以从这个文件中得到它......
request.META.get('HTTP_AUTHORIZATION')
/python3.6/site-packages/rest_framework/authentication.py
you can get that from this file though...
我认为没有任何简单的方法可以只获取 HTTP 标头。您必须迭代 request.META 字典才能获得您需要的所有内容。
django-debug-toolbar 采用相同的方法来显示标头信息。看看这个文件 负责检索标头信息。
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.
django-debug-toolbar takes the same approach to show header information. Have a look at this file responsible for retrieving header information.
如果您想从请求标头获取客户端密钥,您可以尝试以下操作:
If you want to get client key from request header, u can try following:
就其价值而言,您的意图似乎是使用传入的 HTTP 请求来形成另一个 HTTP 请求。有点像网关。有一个出色的模块 django-revproxy 可以完全实现这一点。
该来源是关于如何完成您想要做的事情的一个很好的参考。
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.
The source is a pretty good reference on how to accomplish what you are trying to do.
请注意在标头密钥中使用“-”而不是“_”,
因为如果您在标头中使用“Current_User”,则无法通过
用户“xxx-xxx”作为密钥来获取它
Just be aware to use "-" instead of "_" in header Key
Because if you use "Current_User" in header, you can not get it by
user "xxx-xxx" as the key