200-901 Question 85
Single answerYou are tasked with automating the retrieval of device information from a network using Cisco's RESTCONF API. The requirements specify that you must fetch interface details in JSON format from a device using Python. Which of the following code snippets correctly meets the requirements?
- A
import requests url = 'https://device-ip/restconf/data/interfaces/interface' headers = {'Accept': 'application/json'} response = requests.get(url, headers=headers, auth=('username', 'password')) print(response.json())
- B
import http.client conn = http.client.HTTPSConnection('device-ip') headers = {'Accept': 'application/json'} conn.request('GET', '/restconf/data/interfaces/interface', headers=headers) response = conn.getresponse() print(response.json())
- C
import requests url = 'https://device-ip/restconf/data/interfaces/interface' headers = {'Content-Type': 'application/json'} response = requests.post(url, headers=headers, auth=('username', 'password')) print(response.json())
- D
import requests url = 'https://device-ip/api/interfaces' headers = {'Accept': 'application/json'} response = requests.get(url, headers=headers, auth=('username', 'password')) print(response.text)
Show answer and explanation
Correct answer: A
Explanation
The correct code snippet must use the requests library to make a GET request to the correct RESTCONF endpoint (/restconf/data/interfaces/interface) with the appropriate headers to request JSON data. The response must then be parsed as JSON using response.json(). Option 1 satisfies all the requirements, while the other options either use incorrect methods, endpoints, or request types.
- A. Correct.
This is the correct answer. It uses the
requestslibrary to make a RESTCONF GET request to the appropriate endpoint for interfaces and includes the correct headers to request JSON data. The response is then parsed usingresponse.json(), which meets the requirements. - B. Incorrect.
This option uses the
http.clientlibrary, which is more complex and less commonly used for REST APIs. Additionally, the.json()method is not available in thehttp.clientlibrary, making this code incorrect. - C. Incorrect.
This option makes a POST request instead of a GET request, which does not match the requirement to fetch data. POST requests are typically used to create or update resources, not retrieve them.
- D. Incorrect.
This option uses an incorrect URL (
/api/interfacesinstead of/restconf/data/interfaces/interface) and printsresponse.textinstead of parsing the JSON data. This does not meet the requirements.