300-435 Question 86
Single answerYou are tasked with configuring a Cisco IOS-XE device using the RESTCONF API. You need to use the Python requests library to send a PATCH request to update the hostname of the device. Which code snippet correctly demonstrates this process?
- A
import requests url = 'https://10.10.20.48/restconf/data/Cisco-IOS-XE-native:native/hostname' headers = {'Content-Type': 'application/yang-data+json'} data = {'Cisco-IOS-XE-native:hostname': 'NewHostname'} response = requests.patch(url, headers=headers, data=data, auth=('admin', 'C1sco12345'))
- B
import requests url = 'https://10.10.20.48/restconf/data/Cisco-IOS-XE-native:native/hostname' headers = {'Content-Type': 'application/yang-data+json'} data = {'Cisco-IOS-XE-native:hostname': 'NewHostname'} response = requests.patch(url, headers=headers, json=data, auth=('admin', 'C1sco12345'))
- C
import requests url = 'https://10.10.20.48/restconf/data/Cisco-IOS-XE-native:native/hostname' headers = {'Accept': 'application/yang-data+json'} data = {'Cisco-IOS-XE-native:hostname': 'NewHostname'} response = requests.get(url, headers=headers, json=data, auth=('admin', 'C1sco12345'))
- D
import requests url = 'https://10.10.20.48/restconf/data/Cisco-IOS-XE-native:native/hostname' headers = {'Content-Type': 'application/json'} data = {'Cisco-IOS-XE-native:hostname': 'NewHostname'} response = requests.put(url, headers=headers, json=data, auth=('admin', 'C1sco12345'))
Show answer and explanation
Correct answer: B
Explanation
To configure a device using RESTCONF and the Python requests library, you must send the correct HTTP request with properly formatted headers and payload. PATCH is the appropriate method for partial updates, and the json parameter ensures the payload is serialized as JSON. The correct headers (Content-Type or Accept) are also necessary for RESTCONF APIs to interpret the request properly.
- A. Incorrect.
This option is incorrect because the
dataparameter is used instead ofjson, which means the payload will not be properly serialized to JSON. RESTCONF APIs require properly formatted JSON payloads. - B. Correct.
This is the correct option. It uses the
jsonparameter to ensure the payload is serialized to JSON format, includes the correct headers, and uses thePATCHmethod to update the hostname. - C. Incorrect.
This option is incorrect because the
GETmethod is used instead ofPATCH. TheGETmethod is used to retrieve data, not to update it. - D. Incorrect.
This option is incorrect because the
PUTmethod is used instead ofPATCH, and theContent-Typeheader is not specific to YANG data. WhilePUTcan replace the resource, it is not the recommended method for partial resource updates in this scenario.