如何使用参数传递词典到requests.api.request?

2024-10-01 11:21:25 发布

您现在位置:Python中文网/ 问答频道 /正文

我需要为我的get请求传递一个字典,但是当这样执行时,我得到一个编译错误:

patVect = {"poo": 0, "pin": 0, "pok": 0, "pat": 0}
# querystring = {"patients": "{\"poo\":0, \"pin\":0, \"pok\":0, \"pat\":0}"}
querystring = {"patients": patVect}
headers = {
    'content-type': "application/json",
    'cache-control': "no-cache",
}
response = requests.api.request('get', HURL, headers=headers, params=querystring, verify=False)

当我处理注释查询时,效果很好。知道为什么这不起作用了,或者有什么功能可以帮助你。在


Tags: cacheget字典applicationtype错误pincontent
3条回答

可以使用Postman测试该API,可以在body部分中将dict参数作为原始JSON传递。在

然后,当您测试了API调用之后,就可以继续使用您的首选语言为它生成代码。在

这是Python Requests

import requests

url = "http://localhost:8000/foo/bar/"

payload = "{\n\t\"dict\": {\n\t\t\"key1\": \"value1\",\n\t\t\"key2\": \"value2\"\n\t}\n}"
headers = {
    'Content-Type': "application/json",
    'cache-control': "no-cache",
    'Postman-Token': <TOKEN>
    }

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

print(response.text)

这是Python http.client(Python3)

^{pr2}$

问题是: querystring={“病人”:json.dumps文件(patVect)}

我希望这对您的请求有帮助api

相关问题 更多 >