200-901 Question 43
Single answerA developer is testing a REST API that retrieves user details. When making a request to an endpoint with a user ID that does not exist, the API responds with a status code. What is the most appropriate HTTP response code for this scenario?
- A
200 OK
- B
201 Created
- C
400 Bad Request
- D
404 Not Found
Show answer and explanation
Correct answer: D
Explanation
The 404 Not Found status code is used when a client makes a valid request to the server, but the server cannot locate the requested resource. In this scenario, the user ID does not exist, so 404 is the most appropriate response code. Understanding common HTTP response codes is crucial for effective REST API design and debugging.
- A. Incorrect.
200 OK indicates that the request was successful, and the server returned the requested data. However, in this scenario, the requested resource (user ID) does not exist, so this is not the correct response code.
- B. Incorrect.
201 Created is used when a resource has been successfully created on the server, such as after a POST request. This does not apply to a failed retrieval of a non-existent user.
- C. Incorrect.
400 Bad Request indicates that the request was invalid, such as malformed syntax or missing parameters. In this case, the request is valid but the resource does not exist, so this is not the correct response code.
- D. Correct.
404 Not Found is the correct response code for a situation where the requested resource (e.g., user ID) cannot be found on the server.