简体   繁体   English

如何使用 python 获取客户端在请求标头中插入的新参数?

[英]How to fetch a new param inserted by client in request headers using python?

I have a curl request as:我有一个 curl 请求:

curl --location --request POST 'http://127.0.0.1:8000/hit_api/' \
--header 'x-access-token: xxxx' \
--header 'HTTP_APP: "abcd123"'

Now I want to obtain the string: abcd123 from the HTTP_APP param inserted by the requester.现在我想从请求者插入的HTTP_APP参数中获取字符串: abcd123

I tried with:我试过:

print(request.META)
    
if "HTTP_APP" not in request.META:

   return JsonResponse({
     'success': False,
     'message': 'HTTP_APP not found in Headers'
   })

And I get only the above JsonResponse because it's not printing the HTTP_APP from the headers.而且我只得到上面的 JsonResponse ,因为它没有从标题中打印HTTP_APP

request.META output: request.META .META output:

{'TMPDIR': '/var/folders/07//T/', 'XPC_FLAGS': '0x0', 'TERM_PROGRAM_VERSION': '433', 'TERM_PROGRAM': 'Apple_Terminal', 'XPC_SERVICE_NAME': '0', 'TERM_SESSION_ID': '63385300-42FE-4D88-862B-6A55FD4FE263', 'TERM': 'xterm-256color', '
SSH_AUTH_SOCK': '/private/tmp/com.apple.launchd.bLmUMoeIe0/Listeners', 'SHELL': '/bin/zsh', 'HOME': '/Users/servify', 'LOGNAME': '', 'USER': '', 'PATH': '/Users/servify/Desktop/virtualenvs/crackdenv/bin:/Library/Frameworks/Python.framework/Versions/3.6/bin:/Library/Frameworks/Python.framework/Versions/3.7/bin:/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin', 'SHLVL': '1', 'PWD': '/Users/servify/Desktop/crackd-api', 'OLDPWD': '/Users/Desktop/virtualenvs', 'VIRTUAL_ENV': '/Users/servify/Desktop/virtualenvs/abcenv, 'PS1': '(crackdenv) %n@%m %1~ %# ', 'ABC_ENV': 'development', 'LC_CTYPE': 'UTF-8', '
_': '/Users/servify/Desktop/virtualenvs/abcenv/bin/python', '__CF_USER_TEXT_ENCODING': '0x1F6:0x0:0x0', '__PYVENV_LAUNCHER__': '/Users/Desktop/virtualenvs/crackdenv/bin/python', 'DJANGO_SETTINGS_MODULE': 'server.settings', 'TZ': 'UTC', 'RUN_MAIN': 'true', 'SERVER_NAME': '1.0.0.127.in-addr.arpa', 'GATEWAY_INTERFACE': 'CGI/1.1', 'SERVER_PORT': '8000', 'REMOTE_HOST': '', 'CONTENT_LENGTH': '0', 'SCRIPT_NAME': '', 'SERVER_PROTOCOL': 'HTTP/1.1', 'SERVER_SOFTWARE': 'WSGIServer/0.2', 'REQUEST_METHOD': 'POST', 'PATH_INFO': '/get_config/', 'QUERY_STRING': '', 'REMOTE_ADDR': '127.0.0.1', 
'CONTENT_TYPE': 'text/plain', 'HTTP_X_ACCESS_TOKEN': '', 'HTTP_USER_AGENT': 'PostmanRuntime/7.26.1', 'HTTP_ACCEPT': '*/*', 'HTTP_POSTMAN_TOKEN': '', 'HTTP_HOST': '127.0.0.1:8000', 'HTTP_ACCEPT_ENCODING': 'gzip, deflate, br', 'HTTP_CONNECTION': 'keep-alive', 'wsgi.input': <_io.BufferedReader name=11>, 'wsgi.errors': <_io.TextIOWrapper name='<stderr>' mode='w' encoding='UTF-8'>, 'wsgi.version': (1, 0), 'wsgi.run_once': False, 'wsgi.url_scheme': 'http', 'wsgi.multithread': True, 'wsgi.multiprocess': False, 'wsgi.file_wrapper': <class 'wsgiref.util.FileWrapper'>}

I'm using Django 2.1.2 so cannot use request.headers as per the documentation: https://docs.djangoproject.com/en/3.0/ref/request-response/#attributes我正在使用 Django 2.1.2 所以不能使用request.headers根据文档: https://docs.djangoproject.com/en/3.0/ref/request-response/#attributes

Can someone please guide me on how to get the custom header parameter?有人可以指导我如何获取自定义 header 参数吗?

Thanks谢谢

If you are using django-cors-headers library, you have to first allow custom headers, otherwise they will be neglected.如果您使用 django-cors-headers 库,则必须首先允许自定义标头,否则它们将被忽略。 Add this to your settings file将此添加到您的设置文件

from corsheaders.defaults import default_headers
CORS_ALLOW_HEADERS = list(default_headers) + [
    'X-AUTHORIZATION-KEY',  # whatever your header name is add here(without prefixing HTTP)
]

And also as documented here - https://docs.djangoproject.com/en/2.1/ref/request-response/#django.http.HttpRequest.META也如此处所述 - https://docs.djangoproject.com/en/2.1/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.

Assuming your header name is 'X-AUTHORIZATION-KEY', you will send only 'X-AUTHORIZATION-KEY' from curl/postman request and in your django code check for request_meta.get('HTTP_X_AUTHORIZATION_KEY')假设您的 header 名称是“X-AUTHORIZATION-KEY”,您将仅从 curl/postman 请求中发送“X-AUTHORIZATION-KEY”,并在您的 django 代码中检查request_meta.get('HTTP_X_AUTHORIZATION_KEY')

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 "如何使用 Python 从网络选项卡的特定请求的“请求标头”中获取信息?" - How can I fetch info from the "Request Headers" of a specific request from the network tab using Python? 如何使用python发送包含标头的GET请求 - How to send GET request including headers using python 如何使用python迭代webapp RequestHandler中的所有请求头? - How to iterate over all request headers in webapp RequestHandler using python? 如何使用 python 请求获取请求标头中的令牌 - How do you grab tokens in request headers using python requests 如何使用带有某些标头的Request Python发送文件? - How to send file using Request Python with some headers? 如何在使用连接器/ python驱动程序(MySQL)插入行后立即获取LAST_INSERT_ID() - How to fetch LAST_INSERT_ID() immediately after row is inserted using connector/python driver (MySQL) 如何使用Python请求获取请求标头而不是响应标头 - How to get request headers rather than response headers using Python Requests python请求参数 - python request param 从 Python 客户端发送 Websockets 连接请求中的标头 - Send headers in Websockets connection request from Python client 如何在websocket python客户端中添加更多标头 - How to add more headers in websocket python client
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM