python - 如何从 Django Headers 获取数据?

标签 python django django-rest-framework django-views

我在从 Django header 获取数据时遇到问题。

我的 API 使用 CURL:-

curl -X POST \
  https://xyx.com \
  -H 'Cache-Control: no-cache' \
  -H 'Content-Type: application/json' \
  -H 'xyzId: 3223' \
  -H 'abcData: ABC-123' \
  -d '{
  "name": "xyz",
  "dob": "xyz",
  "user_info": "xyz",
}'

在我的 API 中,我需要获取 xyzIdabcData

我尝试了 request.META['abcData'] 但出现错误 KeyError

如何在我的 View 中获取这两个数据?

请帮我解决这个问题。

提前致谢。

最佳答案

根据文档说 https://docs.djangoproject.com/en/2.0/ref/request-response/#django.http.HttpRequest.META

With the exception of CONTENT_LENGTH and CONTENT_TYPE, as given above, any HTTP headers in the request are converted to META keys by converting all characters to uppercase, replacing any hyphens with underscores and adding an HTTP_ prefix to the name. So, for example, a header called X-Bender would be mapped to the META key HTTP_X_BENDER.

所以你应该能够像这样访问你的标题

request.META['HTTP_ABCDATA']

关于python - 如何从 Django Headers 获取数据?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50584249/

相关文章:

python - SIGINT 在 python 中不可捕获

python - 在 django admin 中动态设置 readonly_fields

django - VS Code 不使用 pipenv .env 文件

python - 在 Django Rest Framework 中使用身份验证测试 POST

django - 如何在 Django Rest 框架可浏览 API 中发送 header

django - 单击电子邮件激活链接后如何将状态传递给前端

python - 如何将关键字附加到 JPG 图像中的 IPTC 数据?

python - tf.layers.conv2d和tf.contrib.slim.conv2d之间的区别

python - Django admin 很难定制吗?

python - Django celery ,导入错误 : Import by filename is not supported