If the response is in json you could do something like (python3):
Copyimport json
import requests as reqs
# Make the HTTP request.
response = reqs.get('https://demo.ckan.org/api/3/action/group_list')
# Use the json module to load CKAN's response into a dictionary.
response_dict = json.loads(response.text)
for i in response_dict:
print("key: ", i, "val: ", response_dict[i])
To see everything in the response you can use .__dict__:
Copyprint(response.__dict__)
Edit in May 2024 to add a suggestion on how to address if objects in the response dict is not JSON serializable.
Copyimport json
...
print(json.dumps(response.text, indent=4, sort_keys=True, default=lambda o:'<not serializable>'))
Answer from Jortega on Stack OverflowVideos
If the response is in json you could do something like (python3):
Copyimport json
import requests as reqs
# Make the HTTP request.
response = reqs.get('https://demo.ckan.org/api/3/action/group_list')
# Use the json module to load CKAN's response into a dictionary.
response_dict = json.loads(response.text)
for i in response_dict:
print("key: ", i, "val: ", response_dict[i])
To see everything in the response you can use .__dict__:
Copyprint(response.__dict__)
Edit in May 2024 to add a suggestion on how to address if objects in the response dict is not JSON serializable.
Copyimport json
...
print(json.dumps(response.text, indent=4, sort_keys=True, default=lambda o:'<not serializable>'))
If you push, for example image, to some API and want the result address(response) back you could do:
Copyimport requests
url = 'https://uguu.se/api.php?d=upload-tool'
data = {"name": filename}
files = {'file': open(full_file_path, 'rb')}
response = requests.post(url, data=data, files=files)
current_url = response.text
print(response.text)
Hi,
I was under the impression that the using .json() to a request made with Python requests, would yield a dictionary object, but it seems I am getting back a string object.
Am I missing a step?
Here is the code:
def otc_holdings():
SIZE = 1
url = 'https://www.otcmarkets.com/research/stock-screener/api'
params = {'securityType' : 'Common Stock,ADRs',
'page':1, 'pageSize': SIZE}
from requests.models import PreparedRequest
req = PreparedRequest()
req.prepare_url(url, params)
print(req.url)
response =requests.get(url,headers = \
USER_AGENTS.random_header(),
params= params).json()
print(response)
print(type(response))
print(response['pages'])
assert response['pages'] == '1'Hi! This is my first post, so let me know if I make any mistake! Also, it's the first time I try to develop anything web using python :) . Anyway, my problem: I'm tryin to develop an app that lets me know when a bus is near my home. I'm taking the gps info from another app, developed by the city. I've been using Fiddler on windows to see how the app comunicates with the remote server. The thing is, I can get the response body from Fiddler manually, but I don't know how to get it from my Python code!
This is a snapshot from Fiddler on my pc.
IMG
Behind the "Save> Response> Response body" you can see the information that I'm trying to get. Any help is useful, thank you so much!
» pip install responses