05-02-2021 11:13 PM
Can someone tell me why this code won't work? Please see the code and error output:
05-03-2021 03:13 AM
Hi Piotr,
Your problem seems to be that you are using a JSON structure as a string into your request, to give you an idea of how you can fix it by either using a string or a JSON payload, I am going to write down here the code for both options.
Both of these options uses a reserved Sandbox from Devnet with IOS-XE code 16.9, you can easily change to another host with the topmost variables
Code using String Payload:
Here the catch is to use a string with the-string-format-method and escape the curly brackets where needed in the final string by doubling them ( { = {{ and } = }} )
import requests import urllib3 urllib3.disable_warnings() credentials = ("developer","C1sco12345") headers = {"Accept":"application/yang-data+json","Content-Type":"application/yang-data+json"} host = "10.10.20.48" port = "443" yang_model_ifc = "ietf-interfaces" container_ifc = "interfaces" url = f"https://{host}:{port}/restconf/data/{yang_model_ifc}:{container_ifc}" payload = ''' {{ "ietf-interfaces:interface": {{ "name": "{name}", "description": "{description}", "type": "iana-if-type:softwareLoopback", "enabled": true, "ietf-ip:ipv4": {{ "address": [ {{ "ip": "{ip}", "netmask": "255.255.255.255" }} ] }}, "ietf-ip:ipv6": {{}} }} }} ''' int_number = 5 for x in range(int_number): response = requests.post(url, auth=credentials, headers=headers, data=payload.format(name="Loopback123"+str(x),description="Added with PYTHON",ip="1.2.3."+str(x)), verify=False) print(f"Status Code: {response.status_code}") print(f"Response Text: {response.text}")
Code using JSON payload
Here payload is a Python dictionary, you can easily reconstruct the format from the previous string payload (they are almost identical). Here the catch is to structure the Python dictionary and then you can modify later the specific values by accessing them by their own keys and then passing the JSON payload into requests.post with the json parameter rather than the data parameter
import requests import urllib3 urllib3.disable_warnings() credentials = ("developer","C1sco12345") headers = {"Accept":"application/yang-data+json","Content-Type":"application/yang-data+json"} host = "10.10.20.48" port = "443" yang_model_ifc = "ietf-interfaces" container_ifc = "interfaces" url = f"https://{host}:{port}/restconf/data/{yang_model_ifc}:{container_ifc}" payload = { "ietf-interfaces:interface": { "name": "", "description": "", "type": "iana-if-type:softwareLoopback", "enabled": True, "ietf-ip:ipv4": { "address": [ { "ip": "", "netmask": "255.255.255.255" } ] }, "ietf-ip:ipv6": {} } } int_number = 5 for x in range(int_number): payload["ietf-interfaces:interface"]["name"] = "Loopback123"+str(x) payload["ietf-interfaces:interface"]["description"] = "Added with PYTHON" payload["ietf-interfaces:interface"]["ietf-ip:ipv4"]["address"][0]["ip"] = "1.2.3."+str(x) #print(payload.format(name="Loopback123"+str(x),description="Added with PYTHON",ip="1.2.3."+str(x))) response = requests.post(url, auth=credentials, headers=headers, json=payload, verify=False) print(f"Status Code: {response.status_code}") print(f"Response Text: {response.text}")
I hope this helps you
05-03-2021 04:16 AM
Hi Giovanni,
My python skills are very low.
Can you use the same sandbox and port as I use and only change the bits in my code that are wrong?
Pointing exactly on the parts of my code that is wrong would be helpful.
Thank you.
05-03-2021 04:29 AM
if you replace your payload variable with this:
payload = ''' {{ "ietf-interfaces:interface": {{ "name": "{name}", "description": "{description}", "type": "iana-if-type:softwareLoopback", "enabled": true, "ietf-ip:ipv4": {{ "address": [ {{ "ip": "{ip}", "netmask": "255.255.255.255" }} ] }}, "ietf-ip:ipv6": {{}} }} }} '''
and in the requests.request , the data=payload with this:
data=payload.format(name="Loopback123"+str(x),description="Added with PYTHON",ip="1.2.3."+str(x))
Most probably it should work
05-03-2021 05:30 AM
Didn't help, unless I've done something wrong. Please see the output:
05-03-2021 05:46 AM
Discover and save your favorite ideas. Come back to expert answers, step-by-step guides, recent topics, and more.
New here? Get started with these tips. How to use Community New member guide