user8817477
user8817477

Reputation: 829

How can I get JSON from request?

How can I get JSON from request?I wrote codes,

@csrf_exempt
def get_json(request):
    print(request)
    return HttpResponse('<h1>OK</h1>')

I post data to POSTMAN like enter image description here

but

print(request) print out WSGIRequest: POST '/app/get_json'. I wanna get json in this part.So I wrote

print(request.text)

but error happens.Why can't I get it?I think request has json data,but is it wrong?How can I do it?

Upvotes: 1

Views: 5629

Answers (1)

Raja Simon
Raja Simon

Reputation: 10305

The raw HTTP request body as a byte string. So use request.body to get all the raw body datas and later convert to json.

json_body = json.loads(request.body)

Upvotes: 6

Related Questions