200-901 · Question #667
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:
The correct answer is requests; Content-Type; status_code; raise. This question assesses the ability to construct a Python script to interact with a REST API using the requests library, covering API calls, header specification, response status checking, and exception handling.
Question
Exhibit
Answer Area
Drag items
Correct arrangement
- requests
- Content-Type
- status_code
- raise
Explanation
This question assesses the ability to construct a Python script to interact with a REST API using the requests library, covering API calls, header specification, response status checking, and exception handling.
Approach. 1. First blank (resp = _______.post(...)): The script imports the requests library. To make a POST request using this library, the post() method is called directly on the requests module. Therefore, 'requests' should be dragged into the first blank.
2. Second blank (headers={'_______ ':'application/json'}}): When sending JSON data in the request body, the standard HTTP header to specify the content type is 'Content-Type', with a value like 'application/json'. Therefore, 'Content-Type' should be dragged into the second blank.
3. Third blank (if resp. _______ != 201:): The resp object is a Response object from the requests library. To check the HTTP status code returned by the API, the status_code attribute is used (e.g., 201 for Created). Therefore, 'status_code' should be dragged into the third blank.
4. Fourth blank (_______ ApiError(...)): This line is within an if block that triggers when the HTTP status code is not 201, indicating an error. In Python, to signal an error by stopping execution and generating an exception, the raise keyword is used. Therefore, 'raise' should be dragged into the fourth blank.
Common mistakes.
- common_mistake. 1. Using 'Application-Type' for headers: 'Application-Type' is not a standard HTTP header. The correct header for specifying the format of the request body is 'Content-Type'.
- Using 'status' for response check: While
requestsresponse objects have various attributes,status_codeis the specific and most common attribute used to retrieve the numerical HTTP status code (e.g., 200, 404, 201). 'status' is not the correct or standard attribute for this purpose. - Using 'return' for error handling: The
returnkeyword is used to exit a function and optionally pass a value back to the caller. It does not raise an exception or indicate an error condition in the same wayraisedoes. To explicitly signal an error and stop execution by throwing an exception,raiseis required.
Concept tested. Python programming with the requests library, REST API consumption, HTTP request methods (POST), HTTP headers (Content-Type), HTTP status codes (201 Created), and Python exception handling (raise keyword).
Topics
Community Discussion
No community discussion yet for this question.
