Response' object is not subscriptable Python http post request
Question:
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"])
Answers:
You should convert your response to a dict:
requestpost = requests.post(url, json=data, auth=(username, password))
res = requestpost.json()
print(res["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"])
the response is not readable so you need to convert that into a readable format,
I was using python http.client
conn = http.client.HTTPConnection('localhost', 5000)
payload = json.dumps({'username': "username", 'password': "password"})
headers = {'Content-Type': 'application/json'}
conn.request('POST', '/api/user/register', payload, headers)
response = conn.getresponse()
print("JSON - ", response.read())
and for request, you can see the answers above
sometimes you have to use json.loads()
function to convert the appropriate format.
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"])
You should convert your response to a dict:
requestpost = requests.post(url, json=data, auth=(username, password))
res = requestpost.json()
print(res["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"])
the response is not readable so you need to convert that into a readable format,
I was using python http.client
conn = http.client.HTTPConnection('localhost', 5000)
payload = json.dumps({'username': "username", 'password': "password"})
headers = {'Content-Type': 'application/json'}
conn.request('POST', '/api/user/register', payload, headers)
response = conn.getresponse()
print("JSON - ", response.read())
and for request, you can see the answers above
sometimes you have to use json.loads()
function to convert the appropriate format.