nerdexam
Cisco

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.

Understanding and Using APIs

Question

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 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

350-901 question #26 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:

  1. Drag try to <item 1>: The requests.get() call and subsequent error checking should be enclosed within a try block. This block attempts to execute the code and catches any exceptions that may arise.
  2. Drag raise_for_status() to <item 2>: After making an HTTP request, response.raise_for_status() is called on the response object (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 an HTTPError exception, allowing the except block to catch it.
  3. Drag except to <item 3>: The line if requests.exceptions.HTTPError as err: immediately following <item 3> indicates that this is where an exception is caught. The except keyword is used to define a block of code that handles specific exceptions raised within the corresponding try block.
  4. Drag 401 to <item 4>: The comment "Check Bearer Token" strongly suggests an authentication failure. HTTP status code 401 Unauthorized is returned when a request lacks valid authentication credentials, such as an incorrect or missing bearer token.
  5. Drag 404 to <item 5>: The comment "Check API Endpoint url" suggests that the requested resource could not be found. HTTP status code 404 Not Found is returned when the server cannot find the requested resource at the specified URL.

Common mistakes.

  • common_mistake. Common mistakes include:
  • Incorrect placement of try and except: Placing except before try, or putting code that could raise an exception outside the try block, would lead to syntax errors or unhandled exceptions.
  • Omitting response.raise_for_status(): If response.raise_for_status() is not called (or equivalent manual status code checks are not performed), the requests library will not automatically raise an HTTPError for 4xx or 5xx responses. Consequently, the except requests.exceptions.HTTPError block would never be triggered for these common API errors, leading to silent failures or incorrect program flow.
  • Swapping 401 and 404: Confusing HTTP status codes would result in misleading error messages. 401 specifically relates to authentication (invalid credentials/token), while 404 relates 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

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

Community Discussion

No community discussion yet for this question.

Full 350-901 Practice