Mar-25-2022, 09:04 PM
(This post was last modified: Mar-25-2022, 09:35 PM by deanhystad.)
You did not specify you were writing a RESTful client. API is a really common term and I don't do a lot of web stuff. The json makes more sense now.
resp = restclient.get('API_Call_URI_here') returns a response, it does not write to the console. If something is being written to the console it is not because of the API call. How are you running this code? If you are running Python from the interactive window, the REPL automatically prints a reply to each command. For your command it would print resp.json. This is an artifact of how you are running the code and not real output.
resp will hold the response to your "get". resp.status_code is the status code for the request. resp.json is a json string with all the interesting stuff. You can convert the json string to a Python dictionary to make it easier to work with.
resp = restclient.get('API_Call_URI_here') returns a response, it does not write to the console. If something is being written to the console it is not because of the API call. How are you running this code? If you are running Python from the interactive window, the REPL automatically prints a reply to each command. For your command it would print resp.json. This is an artifact of how you are running the code and not real output.
resp will hold the response to your "get". resp.status_code is the status code for the request. resp.json is a json string with all the interesting stuff. You can convert the json string to a Python dictionary to make it easier to work with.
import requests import json API_ENDPOINT="https://MyURL.com" restclient = requests.RestClient(API_ENDPOINT, credentials_file='MyCredentialsFile.json', verify=False) resp = restclient.get('API_Call_URI_here') data = json.loads(resp.json) print(data["hosts_list"])This should print:
Output:[
{
"host_name": "TestingHost",
"port_number": 443
}
]
Using the dictionary you can ask for any of the json fields using their key name. These are the keys in the response you posted.Quote:"LINE_1", "LINE_2", "EXAMPLE_3", "created_at", "deleted", "deleted_at", "delta_interval",Are you familiar with Python dictionaries?
"description", "disable_backup"' "hosts_list", "id", "name", "password"' "scope_id", "type"