我正在尝试发布HTTP
请求。我已经设法让代码工作,但我正在努力返回一些结果。
结果如下所示
{
"requestId" : "8317cgs1e1-36hd42-43h6be-br34r2-c70a6ege3fs5sbh",
"numberOfRequests" : 1893
}
我正在尝试获取requestId但我一直收到错误响应'对象不可订阅
import json
import requests
workingFile = 'D:\\test.json'
with open(workingFile, 'r') as fh:
data = json.load(fh)
url = 'http://jsontest'
username = 'user'
password = 'password123'
requestpost = requests.post(url, json=data, auth=(username, password))
print(requestpost["requestId"])
答案 0 :(得分:19)
response
对象包含的信息远多于有效负载。要获取POST请求返回的JSON数据,您必须按照in the example所述访问response.json()
:
requestpost = requests.post(url, json=data, auth=(username, password))
response_data = requestpost.json()
print(response_data["requestId"])
答案 1 :(得分:1)
您应该将您的回复转换为词典:
requestpost = requests.post(url, json=data, auth=(username, password))
res = requestpost.json()
print(res["requestId"])