Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/355.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 如何在params中为requests.api.request传递字典?_Python_Json_Dictionary_Get_Python Requests - Fatal编程技术网

Python 如何在params中为requests.api.request传递字典?

Python 如何在params中为requests.api.request传递字典?,python,json,dictionary,get,python-requests,Python,Json,Dictionary,Get,Python Requests,我需要为我的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/js

我需要为我的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)
当我在处理工作正常的已注释查询时。你知道为什么这不起作用吗,或者一个有帮助的函数。

问题是:
querystring={“patients”:json.dumps(patVect)}

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

然后,当您测试了API调用后,就可以继续使用您喜欢的语言为它生成代码

这是作为Python请求的:

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)

这很奇怪,你能在调用api.request之前调试print querystring并显示它的值吗?你有什么错误?你是对的,谢谢你的提示,问题出在服务器上,我的错误是连接仍然打开。requests.exceptions.ConnectionError:('Connection aborted.',RemoteDisconnected('Remote end closed Connection without response',))最后,我遇到了另一个问题,这个问题过去了,但没有得到我原来的dict{“poo”:0,“pin”:0,“pok”:0,“pat”:0}我只得到了{“poo”,“pin”,“pok”,“pat”}为什么你不
json.dumps(patVect)
import http.client

conn = http.client.HTTPConnection("localhost")

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>
    }

conn.request("GET", "foo,bar,", payload, headers)

res = conn.getresponse()
data = res.read()

print(data.decode("utf-8"))