python不支持下标访问,响应”对象不可下标Python http发布请求

I am trying to post a HTTP request. I have managed to get the code to work but I am struggling returning some of the result.

The result looks like this

{

"requestId" : "8317cgs1e1-36hd42-43h6be-br34r2-c70a6ege3fs5sbh",

"numberOfRequests" : 1893

}

I am trying to get the requestId but I keep getting the error Response' object is not subscriptable

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"])

解决方案

The response object contains much more information than just the payload. To get the JSON data returned by the POST request, you'll have to access response.json() as described in the example:

requestpost = requests.post(url, json=data, auth=(username, password))

response_data = requestpost.json()

print(response_data["requestId"])

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值