django获取请求体数据

def index4(request):
    """获取请求体数据,返回数据结果是QueryDict"""
    print(request.POST)
    """
    打印效果:
    <QueryDict: {'name': ['root'], 'password': ['123456']}>
    
    """

    print(request.body) #  b'{\n\t"title": "\xe5\x9c\xa3\xe8\xaf\x9e\xe6\xa0\x91",\n    "username":"\xe5\x9c\xa3\xe8\xaf\x9e\xe8\x80\x81\xe5\xa4\xb4",\n    "age": 1200\n}'
    """
    打印效果:
    b'{\n\t"title": "\xe5\x9c\xa3\xe8\xaf\x9e\xe6\xa0\x91",\n    "username":"\xe5\x9c\xa3\xe8\xaf\x9e\xe8\x80\x81\xe5\xa4\xb4",\n    "age": 1200\n}'
    """
    import json
    ret = json.loads(request.body)
    print(ret) # {'title': '圣诞树', 'username': '圣诞老头', 'age': 1200}

    return HttpResponse("ok")

 

posted @ 2021-11-28 10:14  urls  阅读(175)  评论(0编辑  收藏  举报