Explore essential concepts of REST and HTTP, covering status codes, pagination techniques, and idempotency principles. This quiz helps reinforce your understanding of effective RESTful API communication and proper use of HTTP standards in web services.
Which HTTP status code is used to indicate a successful standard GET request?
Explanation: A 200 OK status code confirms a successful GET request and that data has been returned. A 400 Bad Request indicates a client-side error. 301 Moved Permanently signals that a resource has changed its address, and 503 Gateway Timeout relates to server availability issues. Only 200 OK communicates a normal, successful operation.
Which HTTP method is considered idempotent according to the specification?
Explanation: PUT is idempotent because making the same request multiple times produces the same result. POST is not idempotent, as it usually creates a new resource each time. PATCH can produce different results if repeated. CONNECT is used to establish a tunnel and is not typically considered idempotent.
If a client sends invalid data in a request to a REST API, which HTTP status code should the server return?
Explanation: 400 Bad Request is the appropriate response for invalid client data, indicating that the server cannot process the request due to client error. 201 Created is for successful resource creation, 302 Found redirects to another URL, and 204 No Content indicates success with no response body. Only 400 addresses the invalid input scenario.
Which query parameters are commonly used for implementing pagination in RESTful APIs?
Explanation: limit and offset are widely used for pagination, controlling how many items to return and where to start. accept and content-type relate to content negotiation, not pagination. host and referer convey request origin, while auth and expires are related to authentication and validity, not data paging.
When is it appropriate for a server to return the HTTP status code 201 Created?
Explanation: 201 Created is used when the server has created a new resource, commonly after POST requests. Redirects should use status codes like 302, unauthorized access uses 401, and server errors typically use codes in the 5xx range. Only the creation of a resource warrants a 201 status.
Which HTTP method is classified as 'safe', meaning it should not have side effects on the server?
Explanation: GET is considered safe since it should not change server state, only retrieve data. DELETE and POST modify resources, making them unsafe. TRACE is used for diagnostics and, while generally non-intrusive, is not classified as safe in the same way GET is.
What HTTP status code should a server use to indicate that a requested resource could not be found?
Explanation: 404 Not Found is the standard response when a resource doesn't exist at the requested URL. 202 Accepted means action is pending, 100 Continue is an interim status, and 409 Conflict means the request conflicts with the current state. Only 404 directly indicates absence.
If you send the same PUT request multiple times to update a resource, what should happen each time according to idempotency?
Explanation: PUT is idempotent, meaning repeated identical requests leave the resource unchanged beyond the first update. POST can create multiple resources. It is not standard for the server to return 403 Forbidden or to reject repeated PUTs as duplicates; instead, it applies the update consistently.
When an API response includes pagination, which of the following is a typical way to indicate there are more results available?
Explanation: APIs often include a next page URL in the response to help clients retrieve additional results. A custom header like Server-Secret isn't a recognized pagination method. 418 is an unrelated novelty status code. Only returning one static page doesn't support pagination.
After a successful PUT request where no changes were made to the resource, what is a common HTTP status code the server might return?
Explanation: 204 No Content is often returned after successful PUT requests when there is no additional information to send back and no new data is provided. 202 Accepted is used for requests accepted but not yet acted upon. 401 means authentication is required, while 501 indicates an unsupported method. 204 most appropriately signals success with no new content.