nerdexam
Cisco

300-435 · Question #116

Refer to the exhibit. An engineer is experiencing issues with their Cisco SDWAN vManage script. Error handling must be added in the script to raise Python exceptions when the HTTP status code is not…

The correct answer is B. response.raise_for_status(). To raise an HTTPError for bad responses (4xx or 5xx status codes) when using the requests library in Python, the raise_for_status() method must be called on the response object.

Controller-Based Network Automation

Question

Refer to the exhibit. An engineer is experiencing issues with their Cisco SDWAN vManage script. Error handling must be added in the script to raise Python exceptions when the HTTP status code is not ok (4xx or 5xx). Which code snippet must be added to the box in the code to perform this task?
import requests

vmanage_host = "sandbox-sdwan-1.cisco.com"
token = "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpYXQiOjE2ODMxMzYyNTQsImV4cCI6MTY4MzEzOTg1NCwiaXNzIjoic2FuZGJveC1zZHdhbi0xLmNpc2NvLmNvbSIsImp0aSI6ImZhMWQ4MDViMi03MzI1LTQzYWItYjFhZi0wMjAwMDlmMjgyMjYifQ.F3DD134D93C1C2F29F8F8F43AF7F422F0"
session_id = "NTYzNmU3ZDJiMjAzMDQ2N2FkNzcxMDQzNzEyYTFhNGU4MjAxMTA4MzBiNTMxMmFjMDcxODUzNzIyNzE5MjQ0MzU4"

url = f"https://{vmanage_host}/dataservice/system/device/management/systemip"
headers = {
 "Content-Type": "application/json",
 "X-XSRF-TOKEN": token,
 "Cookie": f"JSESSIONID={session_id}",
}

# MISSING LINE HERE

print(response.json())

Exhibit

300-435 question #116 exhibit

Options

  • Arequests.raise_for_status()
  • Bresponse.raise_for_status()
  • Cresponse.status_code
  • Drequests.codes.ok

How the community answered

(23 responses)
  • A
    4% (1)
  • B
    74% (17)
  • C
    9% (2)
  • D
    13% (3)

Why each option

To raise an HTTPError for bad responses (4xx or 5xx status codes) when using the `requests` library in Python, the `raise_for_status()` method must be called on the `response` object.

Arequests.raise_for_status()

`requests.raise_for_status()` is incorrect because `raise_for_status()` is a method of a `Response` object, not directly a method of the `requests` module itself.

Bresponse.raise_for_status()Correct

The `response.raise_for_status()` method, called on a `requests` library response object, is specifically designed to raise an `HTTPError` exception if the HTTP request returned an unsuccessful status code (i.e., 4xx client error or 5xx server error). This provides a convenient way to implement error handling for API calls.

Cresponse.status_code

`response.status_code` only returns the integer status code and does not raise an exception, so it does not fulfill the requirement of raising a Python exception for error codes.

Drequests.codes.ok

`requests.codes.ok` is used to check if a status code is 200 (OK) but does not raise an exception for error status codes.

Concept tested: Python requests library error handling

Source: https://docs.python-requests.org/en/latest/api/#requests.Response.raise_for_status

Topics

#Python#requests library#Error Handling#HTTP Status Codes

Community Discussion

No community discussion yet for this question.

Full 300-435 Practice