Django request Post json

后端 未结 4 2059
孤独总比滥情好
孤独总比滥情好 2021-01-01 19:35

I try to test a view, I receive a json request from the IPad, the format is:

req = {\"custom_decks\": [
        {
            \"deck_name\": \"deck_test\",
          


        
4条回答
  •  礼貌的吻别
    2021-01-01 19:50

    As I was having problems with getting JSON data from HttpRequest directly with the code of the other answer:

    data = json.loads(request.body)
    custom_decks = data['custom_decks']
    

    error:

    the JSON object must be str, not 'bytes'
    

    Here is an update of the other answer for Python version >3:

    json_str=((request.body).decode('utf-8'))
    json_obj=json.loads(json_str)
    

    Regarding decode('utf-8'), as mention in:

    RFC 4627:

    "JSON text shall be encoded in Unicode. The default encoding is UTF-8."

    I attached the Python link referred to this specific problem for version >3.

    http://bugs.python.org/issue10976

提交回复
热议问题