Courseiva
Automation →hardMultiple Choice

350-401 Automation Practice Question

A network engineer is implementing a Python script to interact with a Cisco IOS XE device using RESTCONF. The script must authenticate using basic authentication over HTTPS and retrieve the configured hostname. Which Python code snippet correctly performs this task using the requests library?

⚠ Common exam trap

The trap here is using Content-Type instead of Accept for a GET request, or forgetting the namespace prefix in the JSON key, both of which cause failures.

Answer choices

Why each option matters

Answer the question above first, then reveal the full breakdown to understand why each option is right or wrong.

Correct answer & explanation

✓

import requests url = 'https://192.168.1.1/restconf/data/Cisco-IOS-XE-native:native/hostname' headers = {'Accept': 'application/yang-data+json'} response = requests.get(url, headers=headers, auth=('admin', 'password'), verify=False) hostname = response.json()['Cisco-IOS-XE-native:hostname']

The correct RESTCONF request must use the proper URL for the hostname leaf, set the Accept header to 'application/yang-data+json', provide basic authentication, and parse the response using the fully qualified key 'Cisco-IOS-XE-native:hostname'. The other options use the wrong URL, wrong header, wrong JSON key, or omit authentication.

Answer analysis

Option-by-option breakdown

For each option: why learners choose it and why it is or isn't the right answer here.

  • ✗

    import requests url = 'https://192.168.1.1/restconf/data/ietf-interfaces:interfaces/interface=GigabitEthernet1' headers = {'Accept': 'application/yang-data+json'} response = requests.get(url, headers=headers, auth=('admin', 'password'), verify=False) hostname = response.json()['name']

    Why it's wrong here

    This URL retrieves interface information, not the hostname. The response would contain interface details, and the key 'name' would give the interface name, not the device hostname. This does not meet the requirement of retrieving the configured hostname. The URL path is for interfaces, not the native hostname.

  • ✗

    import requests url = 'https://192.168.1.1/restconf/data/Cisco-IOS-XE-native:native/hostname' headers = {'Accept': 'application/yang-data+json'} response = requests.get(url, headers=headers, verify=False) hostname = response.json()['Cisco-IOS-XE-native:hostname']

    Why it's wrong here

    This snippet omits authentication credentials. RESTCONF on Cisco IOS XE requires authentication; without it, the request will return a 401 Unauthorized error. The URL and headers are correct, but the lack of auth makes it fail. Basic authentication must be provided via the auth parameter or a session.

  • ✗

    import requests url = 'https://192.168.1.1/restconf/data/Cisco-IOS-XE-native:native/hostname' headers = {'Content-Type': 'application/yang-data+json'} response = requests.get(url, headers=headers, auth=('admin', 'password'), verify=False) hostname = response.json()['hostname']

    Why it's wrong here

    This uses the Content-Type header instead of Accept. For a GET request, Accept is used to specify the desired response format. Content-Type is for the request body, which is not present here. Also, the JSON key is missing the 'Cisco-IOS-XE-native:' prefix, so the key would be incorrect. This snippet would likely fail to parse the hostname.

  • ✓

    import requests url = 'https://192.168.1.1/restconf/data/Cisco-IOS-XE-native:native/hostname' headers = {'Accept': 'application/yang-data+json'} response = requests.get(url, headers=headers, auth=('admin', 'password'), verify=False) hostname = response.json()['Cisco-IOS-XE-native:hostname']

    Why this is correct

    This snippet uses the correct RESTCONF URL for the hostname, sets the Accept header to 'application/yang-data+json', provides basic authentication credentials, and disables SSL verification. The JSON response for this leaf is a dictionary with the key 'Cisco-IOS-XE-native:hostname'. This correctly retrieves the hostname.

Go deeper

Related to this question

About these practice questions

Courseiva writes every 350-401 question from scratch — 1,923 in total, each with an explanation and a wrong-answer breakdown. None are copied from real exams or dumps. Learn why practice questions differ from exam dumps →

How Courseiva writes practice questions · Editorial policy

JA

Written and reviewed by Johnson Ajibi, MSc IT Security

Senior Network & Security Engineer · founder of Courseiva

Last reviewed September 2026 · checked against the official Cisco exam blueprint

This 350-401 practice question is part of Courseiva's free Cisco certification practice question bank. Courseiva provides original exam-style practice questions with explanations, topic-based practice, mock exams, readiness tracking, and study analytics to help learners prepare for the 350-401 exam.