nerdexam
CiscoCisco

200-901 · Question #444

200-901 Question #444: Real Exam Question with Answer & Explanation

The question tests the ability to construct a Python script using the 'requests' library to make a REST API POST call, handle the response status, and parse JSON data.

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 construct a Python script that calls a REST API request. Not all options are used. Answer:

Explanation

The question tests the ability to construct a Python script using the 'requests' library to make a REST API POST call, handle the response status, and parse JSON data.

Approach. The correct interaction involves dragging the appropriate code snippets into the four blank spaces:

  1. response = requests .post(...): The import requests statement at the top indicates that the requests library is being used. To call the post method, it must be invoked on the requests object itself. Therefore, 'requests' should be dragged into the first blank.
  2. response = requests.post('...', json=data ): The data variable is a Python dictionary intended to be sent as JSON in the POST request body. The requests library's post method accepts a json parameter for this purpose. Therefore, 'json=data' should be dragged into the second blank.
  3. if response. status_code != 201:: This line is checking the HTTP status code returned by the API call. The requests library stores the HTTP status code in the status_code attribute of the response object. Therefore, 'status_code' should be dragged into the third blank.
  4. for item in response .json():: This loop iterates over the JSON content received in the API response. The json() method is called on the response object itself to parse its JSON body. Therefore, 'response' should be dragged into the fourth blank.

Common mistakes.

  • common_mistake. Common mistakes include:
  • Using 'data=data' instead of 'json=data' for JSON payloads: While data=data can send form-encoded data, for sending JSON, the requests library specifically uses the json parameter to automatically serialize the Python dictionary to a JSON string and set the 'Content-Type: application/json' header. Using data=data for a dictionary would send it as 'application/x-www-form-urlencoded' unless explicitly serialized.
  • Using 'status' instead of 'status_code': The requests library's response object has an attribute named status_code (an integer) for the HTTP status code, not status.
  • Using 'items' or 'headers' inappropriately: 'items' is a method on dictionaries, not directly related to the requests object for iteration in this context. 'headers' is an attribute of the response object containing HTTP headers, not the main data payload for iteration or the object to call .post() on.
  • Calling .json() on 'data' or 'items': data is the input payload, not the response. 'items' is not a valid object here to call .json() on; .json() must be called on the response object.

Concept tested. This question tests the practical application of the Python 'requests' library for making HTTP POST requests, sending JSON data, handling HTTP response status codes, and parsing JSON responses from a REST API.

Topics

#Python#REST API#HTTP requests#API consumption

Community Discussion

No community discussion yet for this question.

Full 200-901 PracticeBrowse All 200-901 Questions