350-901 · Question #26
Complete the Python script by dragging the correct code snippets from the left (Answer Area) into the placeholders <item X> on the right. The script imports requests, defines a URL and headers, and…
The question tests the ability to correctly implement error handling for API calls using Python's requests library, specifically employing try-except blocks, raise_for_status(), and understanding common HTTP status codes for troubleshooting.
Question
<item X> on the right. The script imports requests, defines a URL and headers, and handles HTTP responses. Exhibit:
import requests
url = "https://api.ciscospark.com/v1/rooms"
bearer = "BEARER_TOKEN_HERE"
headers = {"content-type": "application/json", "Authorization": "Bearer " + bearer}
<item 1>:
response = requests.get(url, headers=headers, verify=False)
<item 2>
<item 3>:
if requests.exceptions.HTTPError as err:
if response.status_code == <item 4>:
print("Check Bearer Token")
elif response.status_code == <item 5>:
print("Check API Endpoint url")
elif response.status_code == 500:
print("Server Error, Try again later")
else:
print("HTTP Error") + str(err)
Available options for dragging (from the Answer Area on page 1): 401, 404, try, except, raise_for_status().Exhibit
Explanation
The question tests the ability to correctly implement error handling for API calls using Python's requests library, specifically employing try-except blocks, raise_for_status(), and understanding common HTTP status codes for troubleshooting.
Approach. To correctly complete the Python script for robust API error handling, the following interactions are required:
- Drag
tryto<item 1>: Therequests.get()call and subsequent error checking should be enclosed within atryblock. This block attempts to execute the code and catches any exceptions that may arise. - Drag
raise_for_status()to<item 2>: After making an HTTP request,response.raise_for_status()is called on theresponseobject (implicitly, as the placeholder is for the snippet) to check if the request was successful. If the response status code is a client error (4xx) or server error (5xx), this method will raise anHTTPErrorexception, allowing theexceptblock to catch it. - Drag
exceptto<item 3>: The lineif requests.exceptions.HTTPError as err:immediately following<item 3>indicates that this is where an exception is caught. Theexceptkeyword is used to define a block of code that handles specific exceptions raised within the correspondingtryblock. - Drag
401to<item 4>: The comment "Check Bearer Token" strongly suggests an authentication failure. HTTP status code401 Unauthorizedis returned when a request lacks valid authentication credentials, such as an incorrect or missing bearer token. - Drag
404to<item 5>: The comment "Check API Endpoint url" suggests that the requested resource could not be found. HTTP status code404 Not Foundis returned when the server cannot find the requested resource at the specified URL.
Common mistakes.
- common_mistake. Common mistakes include:
- Incorrect placement of
tryandexcept: Placingexceptbeforetry, or putting code that could raise an exception outside thetryblock, would lead to syntax errors or unhandled exceptions. - Omitting
response.raise_for_status(): Ifresponse.raise_for_status()is not called (or equivalent manual status code checks are not performed), therequestslibrary will not automatically raise anHTTPErrorfor 4xx or 5xx responses. Consequently, theexcept requests.exceptions.HTTPErrorblock would never be triggered for these common API errors, leading to silent failures or incorrect program flow. - Swapping
401and404: Confusing HTTP status codes would result in misleading error messages.401specifically relates to authentication (invalid credentials/token), while404relates to resource availability (incorrect URL or non-existent endpoint). Providing the wrong troubleshooting advice based on the status code would hinder effective debugging.
Concept tested. Python requests library for HTTP requests, API error handling best practices, try-except blocks for exception management, the purpose of requests.Response.raise_for_status(), and understanding common HTTP status codes (401 Unauthorized, 404 Not Found, 500 Internal Server Error) in the context of API interaction and troubleshooting.
Topics
Community Discussion
No community discussion yet for this question.
