Hello, everyone.
Here is my code to retrieve interface information from a device. I have some questions about it.
import requests
from rich import print as rprint
device_url = "https://192.168.170.200:443/restconf/data/ietf-interfaces:interfaces"
headers = {
"Accept": "application/yang-data+json"
}
response = requests.get(url=device_url, headers=headers, auth=("admin", "cisco"), verify=False)
rprint(response.json())
This returns the following output:
{
'ietf-interfaces:interfaces': {
'interface': [
{'name': 'GigabitEthernet1', 'type': 'iana-if-type:ethernetCsmacd', 'enabled': True, 'ietf-ip:ipv4': {'address': [{'ip': '192.168.170.200', 'netmask': '255.255.255.0'}]}, 'ietf-ip:ipv6': {}},
{'name': 'GigabitEthernet2', 'type': 'iana-if-type:ethernetCsmacd', 'enabled': False, 'ietf-ip:ipv4': {}, 'ietf-ip:ipv6': {}},
{'name': 'GigabitEthernet3', 'type': 'iana-if-type:ethernetCsmacd', 'enabled': False, 'ietf-ip:ipv4': {}, 'ietf-ip:ipv6': {}},
{'name': 'GigabitEthernet4', 'type': 'iana-if-type:ethernetCsmacd', 'enabled': False, 'ietf-ip:ipv4': {}, 'ietf-ip:ipv6': {}},
{'name': 'Loopback1', 'type': 'iana-if-type:softwareLoopback', 'enabled': True, 'ietf-ip:ipv4': {'address': [{'ip': '1.1.1.1', 'netmask': '255.255.255.255'}]}, 'ietf-ip:ipv6': {}}
]
}
}
My question is, why doesn’t the .json() function work if I don’t specify an accept header? It just returns an error but the code works if I remove it. Does the device not return the data in a JSON format if I don’t specify the header?
My second question is this. Say that I want to configure another loopback. In the output above, we can see that this configuration is located under ietf-interfaces in the interfaces container and the leaf-list that contains these interfaces is called interface. So I edited my configuration to look like this:
import requests
from rich import print as rprint
device_url = "https://192.168.170.200:443/restconf/data/ietf-interfaces:interfaces/interface"
headers = {
"Accept": "application/yang-data+json",
"Content-Type": "application/yang-data+json"
}
payload = {
'ietf-interfaces:interface': [
{'name': 'Loopback2', 'type': 'iana-if-type:softwareLoopback', 'enabled': True, 'ietf-ip:ipv4': {'address': [{'ip': '2.2.2.2', 'netmask': '255.255.255.255'}]}, 'ietf-ip:ipv6': {}}
]
}
response = requests.post(url=device_url, headers=headers, json=payload, auth=("admin", "cisco"), verify=False)
rprint(response.json())
Yet I keep getting this error
{'errors': {'error': [{'error-message': 'POST on list must be on list element', 'error-path': '/ietf-interfaces:interfaces/interface', 'error-tag': 'malformed-message', 'error-type': 'application'}]}}
If I change the url from
device_url = https://192.168.170.200:443/restconf/data/ietf-interfaces:interfaces/interface
to
device_url = https://192.168.170.200:443/restconf/data/ietf-interfaces:interfaces
It works… and I don’t quite get why. The interfaces reside under ietf-interfaces:interfaces/interface - inside the interface list. Yet I cannot send any POST requests to it, I must go a step back and refer to the container as a whole… why? It’s a little confusing.
Thank you.
David