私のPython API内からのPOST要求で値にアクセスする方法がわかりません。リクエスト内の値にアクセスできない
APIリクエストの値にアクセスできないのはなぜですか?私は明らかにrequest.bodyにそれらを取得していますが、それらを取得できません。ここで
は、JSONのように見えるしていませんrequest.body、次のとおりです。
request.POST['username']
request.POST.get('username')
私はdjango.utils.datastructures.MultiValueDictKeyErrorを示すエラーを受け取る:
私は、次の方法を試してみましたまったく。
"{\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'})
import json
body = json.loads(request.POST)
を私はDjangoはこれを自動的に扱うべきであると思うだろうが:
MultiValueKeyDictエラーのスタックトレースを含めることができますか? –