300-435 Question 87
Single answerYou are tasked with configuring a Cisco device using the RESTCONF API and Python's 'requests' library. You need to send a request to update the hostname of the device. Which of the following code snippets correctly sets the hostname to 'NewDevice' using RESTCONF?
- A
import requests url = 'https://192.168.1.1/restconf/data/Cisco-IOS-XE-native:native/hostname' data = {'Cisco-IOS-XE-native:hostname': 'NewDevice'} headers = {'Content-Type': 'application/json'} response = requests.post(url, json=data, headers=headers, auth=('admin', 'password'))
- B
import requests url = 'https://192.168.1.1/restconf/data/Cisco-IOS-XE-native:native/hostname' data = {'Cisco-IOS-XE-native:hostname': 'NewDevice'} headers = {'Content-Type': 'application/yang-data+json'} response = requests.put(url, json=data, headers=headers, auth=('admin', 'password'))
- C
import requests url = 'https://192.168.1.1/restconf/data/Cisco-IOS-XE-native:native/hostname' data = {'hostname': 'NewDevice'} headers = {'Content-Type': 'application/yang-data+xml'} response = requests.put(url, data=data, headers=headers, auth=('admin', 'password'))
- D
import requests url = 'https://192.168.1.1/restconf/data/Cisco-IOS-XE-native:native/hostname' data = {'Cisco-IOS-XE-native:hostname': 'NewDevice'} headers = {'Content-Type': 'application/yang-data+json'} response = requests.patch(url, json=data, headers=headers, auth=('admin', 'password'))
Show answer and explanation
Correct answer: B
Explanation
The correct answer is the second option because it adheres to the RESTCONF standards for updating resources. It uses the PUT method, which is suitable for replacing or creating the resource at the target URI. The payload structure is accurate, and the 'application/yang-data+json' content type is specified, which is required for RESTCONF API operations. Other options either use the wrong HTTP method, incorrect payload structure, or unsupported content types.
- A. Incorrect.
This code snippet incorrectly uses the POST method. RESTCONF typically uses PUT for creating or updating a specific resource like hostname.
- B. Correct.
This snippet is correct as it uses the PUT method, the appropriate URL, the correct JSON payload structure, and the proper 'application/yang-data+json' content type header.
- C. Incorrect.
This code uses an incorrect payload structure ('hostname' key instead of 'Cisco-IOS-XE-native:hostname') and an unsupported content type ('application/yang-data+xml').
- D. Incorrect.
This code snippet uses the PATCH method, which is not the most appropriate choice for updating the hostname. PUT is preferred for this operation.