我不理解如何從我的Python API中訪問POST請求中的值。無法訪問請求中的值
爲什麼我不能訪問我的API請求中的值?我明顯在request.body中獲取它們,但無法檢索它們。
我曾嘗試以下方法:
request.POST['username']
request.POST.get('username')
我收到一個錯誤,說明django.utils.datastructures.MultiValueDictKeyError:
這裏是request.body,這似乎並不像JSON在所有。
"{\n \"username\": \"TestUsername\",\n \"password\": \"TestPass\"\n}"
POST請求
{
"username": "TestUsername",
"password": "TestPass"
}
HEADERS
Accept: application/json
Content-Type: application/json
VIEW
@csrf_exempt
@api_view(['POST'])
def create(request):
user = User()
if 'username' in request.POST and 'password' in request.POST:
user.username = request.POST['username']
user.set_password(request.POST['password'])
user.save()
return Response({'Status' : 'Complete'})
else:
return Response({'Status': 'Incomplete'})
您能否包含MultiValueKeyDict錯誤的堆棧跟蹤? –