本文介绍了如何将request.data转换为dict?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

限时删除!!

我尝试使用以下行(请求库)POST请求从客户端获取JSON数据:

I try to get JSON data from client using this line (requests library) POST request:

request.data

如何将其转换为字典?

有效:

response_data = request.get_json()

但是如何将其转换为字典?

But how convert this to dict?

推荐答案

对于发布请求:

import requests
import json
url = "https://jsonplaceholder.typicode.com/posts/"

payload = {
    "userId": 10,
    "id": 901,
    "title": "sunt aut facere repellat provident occaecati excepturi optio reprehenderit",
    "body": "quia et suscipit\nsuscipit recusandae consequuntur expedita et cum\nreprehenderit molestiae ut ut quas totam\nnostrum rerum est autem sunt rem eveniet architecto"
 }
headers = {
    'content-type': "application/json",
    'cache-control': "no-cache",
    'postman-token': "c71c65a6-07f4-a2a4-a6f8-dca3fd706a7a"
    }

response = requests.request("POST", url, data=json.dumps(payload), headers=headers)

print(type(response.json()))


您可以使用以下内容:


You can use something like this:

import requests

url = "https://api.icndb.com/jokes/random"

headers = {
    'cache-control': "no-cache",
    'postman-token': "77047c8b-caed-2b2c-ab33-dbddf52a7a9f"
    }

response = requests.request("GET", url, headers=headers)

print(type(response.json()))

这篇关于如何将request.data转换为dict?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

1403页,肝出来的..

09-08 09:44