300-435 · Question #151
Refer to the exhibit. An engineer must create a Cisco SDWAN vManage script with error handling to raise Python exceptions when the HTTP status code is not ok. Which code snippet must be added to the…
The correct answer is B. response = requests.request("GET", url, headers=headers, verify=False) response.raise_for_status() print(response.json()). The raise_for_status() method must be called on the response object instance, not on the requests module itself, and a GET method is appropriate for retrieving data.
Question
Options
- Aresponse = requests.request("GET", url, headers=headers, verify=False) requests.raise_for_status() print(response.json())
- Bresponse = requests.request("GET", url, headers=headers, verify=False) response.raise_for_status() print(response.json())
- Cresponse = requests.request("PUT", url, headers=headers, verify=False) requests.raise_for_status() print(response.json())
- Dresponse = requests.request("PUT", url, headers=headers, verify=False) requests.raise_for_status() print(response.json())
How the community answered
(30 responses)- B93% (28)
- C3% (1)
- D3% (1)
Why each option
The raise_for_status() method must be called on the response object instance, not on the requests module itself, and a GET method is appropriate for retrieving data.
requests.raise_for_status() attempts to call raise_for_status as a module-level function on the requests library, which does not exist and will raise an AttributeError.
raise_for_status() is an instance method of the requests.Response object and must be called as response.raise_for_status(), not requests.raise_for_status(). Using GET is correct for retrieving vManage data without modifying it. This method raises an HTTPError exception for 4xx and 5xx status codes, enabling proper error handling.
PUT is used to update or replace a resource, not to retrieve data - using PUT for a read operation is semantically incorrect, and requests.raise_for_status() is also invalid.
PUT is the wrong HTTP method for retrieving data, and requests.raise_for_status() is not a valid call on the module itself.
Concept tested: Python requests response.raise_for_status() error handling
Source: https://requests.readthedocs.io/en/latest/api/#requests.Response.raise_for_status
Topics
Community Discussion
No community discussion yet for this question.