Python 如何从POST请求中获取auth参数?

Python 如何从POST请求中获取auth参数?,python,django,python-requests,Python,Django,Python Requests,我向自己发送了一个POST请求: requests.post(“http://localhost:8000/api/,json=data,auth=('myuser','mypwd')) 我正在使用Django接收POST请求。我想验证用户名和密码是否与我拥有的匹配 def myAPI(request) if request.method == 'POST': # obtain username and password here 如何以明文形式获取用户名和密码?

我向自己发送了一个POST请求:

requests.post(“http://localhost:8000/api/,json=data,auth=('myuser','mypwd'))

我正在使用Django接收POST请求。我想验证用户名和密码是否与我拥有的匹配

def myAPI(request)
    if request.method == 'POST':

          # obtain username and password here

如何以明文形式获取用户名和密码?我试过
request.body.decode()
request.POST.items()
request.META
——就是找不到!我可以在
数据中发送凭证,这将很容易。或者,我可以使用API密钥而不是用户名和密码,但我也不知道如何访问它们。

这将适用于您:

req_header = request.META['HTTP_AUTHORIZATION']
credentials = auth_header.split(' ')[1]
base64_decoded_credentials = base64.b64decode(encoded_credentials)
decoded_utf_credentials = base64_decoded_credetials.decode("utf-8").split(':')
username = decoded_utf_credentials[0]
password = decoded_utf_credentials[1]

这将对您有用:

req_header = request.META['HTTP_AUTHORIZATION']
credentials = auth_header.split(' ')[1]
base64_decoded_credentials = base64.b64decode(encoded_credentials)
decoded_utf_credentials = base64_decoded_credetials.decode("utf-8").split(':')
username = decoded_utf_credentials[0]
password = decoded_utf_credentials[1]

它应该在
request.META['HTTP_AUTHORIZATION']
中,当我打印
print('Auth:',request.META['HTTP_AUTHORIZATION'])
时,它打印类似于
Auth:Basic bX85js03jg9sl
(随机字符)Aha的内容——它是用base64编码的。在另一个上找到。它不是随机的。这是您的用户名和密码,base64编码。为什么在所有其他信息都没有的情况下它是base64编码的?当我打印
print('Auth:',request.META['HTTP_AUTHORIZATION'])时,它应该在
request.META['HTTP_AUTHORIZATION']
中打印类似于
Auth:Basic bX85js03jg9sl
(随机字符)啊哈——它是用base64编码的。在另一个上找到。它不是随机的。这是您的用户名和密码,base64编码。为什么所有其他信息都不是base64编码的呢?