nerdexam
Cisco

200-901 · Question #664

Drag and Drop Question Drag and drop the code from the bottom onto the box where the code is missing to output the status of the REST API response from the script. Some options may be used more than…

The correct answer is HTTPBasicAuth; HTTPBasicAuth; get; status_code. This question assesses the test-taker's proficiency in using Python's requests library to perform a basic authenticated GET request to a REST API and retrieve its HTTP status code.

Understanding and Using APIs

Question

Drag and Drop Question Drag and drop the code from the bottom onto the box where the code is missing to output the status of the REST API response from the script. Some options may be used more than once. Not all options are used. Answer:

Exhibit

200-901 question #664 exhibit

Answer Area

Drag items

status_codestatusHTTPBasicAuthHTTPTokengetpost

Correct arrangement

  • HTTPBasicAuth
  • HTTPBasicAuth
  • get
  • status_code

Explanation

This question assesses the test-taker's proficiency in using Python's requests library to perform a basic authenticated GET request to a REST API and retrieve its HTTP status code.

Approach. The correct interaction involves dragging and dropping the following code snippets into their respective blanks:

  1. from requests.auth import HTTPBasicAuth: The line auth = _____ ('apiUser', '1234abcf') indicates that an authentication class is being instantiated with a username and password. HTTPBasicAuth is the correct class for standard HTTP Basic Authentication from the requests.auth module.
  2. auth = HTTPBasicAuth ('apiUser', '1234abcf'): Following the import, the HTTPBasicAuth class is instantiated with the provided username and password to create an authentication object.
  3. response = requests.get (url, headers=headers, auth=auth): The scenario describes an API call to a URL with headers and authentication. Without any explicit instruction to send a body or create/update resources, a standard request to retrieve information from an API is typically a GET request. The requests.get() method is used for this purpose.
  4. print(response.status_code): The question asks to 'output the status of the REST API response'. The status_code attribute of the requests library's Response object provides the integer HTTP status code (e.g., 200, 404, 500), which is the most accurate way to report the response status.

Common mistakes.

  • common_mistake. Common mistakes include:
  • Using HTTPToken instead of HTTPBasicAuth: HTTPToken is used for token-based authentication (like Bearer tokens) and not for simple username/password pairs as shown in the example.
  • Using post instead of get: A POST request is generally used to send data to the server to create or update a resource. Since there's no data payload or specific intent to modify resources mentioned, GET is the appropriate method for fetching information.
  • Using status instead of status_code: The requests library Response object does not expose a status attribute to get the HTTP status code directly. Instead, response.status_code returns the numerical status (e.g., 200, 404), and response.reason returns the textual reason (e.g., 'OK', 'Not Found'). status_code is the precise attribute needed here.

Concept tested. The core concept tested is the ability to write Python code using the requests library to interact with REST APIs, specifically focusing on HTTP Basic Authentication, making GET requests, and retrieving the HTTP status code from the response object.

Topics

#API Response Handling#REST API#Scripting#HTTP Status Codes

Community Discussion

No community discussion yet for this question.

Full 200-901 Practice