Test your knowledge of HTTP and REST fundamentals, including status codes, pagination techniques, and the principles of idempotency. This quiz is designed for learners seeking to understand essential concepts for building and consuming web APIs effectively.
What does the HTTP status code 200 OK signify when a client sends a GET request to an API endpoint?
Explanation: The status code 200 OK is sent when a request has succeeded and the server returns the requested resource. A 404 code means the resource was not found, which is different. A 500 status code indicates an internal server error, and 401 means the client must authenticate themselves, not that the request succeeded.
Which HTTP status code should a server return if a client requests a resource that does not exist?
Explanation: When a resource is not available on the server, a 404 Not Found response is appropriate. The 201 Created status is used when something new is created, not for missing resources. 301 indicates a permanent redirect, which doesn’t fit this case. 202 Accepted means the request has been received but not yet processed.
Which HTTP method is typically used to submit data that creates a new resource, such as submitting a new user registration form?
Explanation: POST is the method designed for creating new resources by submitting data to the server. GET is used for retrieving information, not submitting data that will change the server state. DELETE removes resources, while PATCH is intended for partial updates.
Which of the following HTTP methods is considered idempotent when interacting with an API endpoint?
Explanation: PUT is idempotent because calling it multiple times with the same data will always result in the resource having the same state. POST is not idempotent because multiple POSTs can create multiple resources. CONNECT establishes a tunnel, which is not idempotent, and OPTIONS is used to describe communication options and doesn't actually modify resources.
If an API successfully creates a new resource upon receiving data, which status code should it return?
Explanation: A 201 Created response is appropriate when a resource has been successfully created. 204 No Content would indicate that the request was successful but there is no response body, which is not as descriptive. 301 is for redirection, and 403 means the client is not allowed to access the resource.
Why is pagination commonly used when retrieving a large list of resources from a REST API?
Explanation: Pagination helps prevent overwhelming both the server and clients by limiting the amount of data returned in a single response, making data retrieval more efficient. It is not used for authentication, nor does it relate to encrypting responses or handling simultaneous resource creation and deletion.
After a successful DELETE request, which HTTP status code is most appropriate if no further content needs to be returned?
Explanation: 204 No Content is used to indicate that the request was successful, but the server is not returning any content. 403 would mean the client lacks permission, 302 is for temporary redirects, and 412 refers to preconditions that failed, none of which apply to a completed DELETE action.
Which HTTP status code indicates that there was something wrong with the client's request, such as missing required fields or malformed JSON?
Explanation: The 400 Bad Request code is used when the server cannot understand the request due to invalid syntax or missing data. 406 means the server cannot send the requested content type, 307 is for redirects, and 504 concerns gateway timeouts, not client request errors.
Which situation best matches returning a 401 Unauthorized status code from a REST API?
Explanation: 401 Unauthorized is used when authentication is required and has failed or has not yet been provided. Unsupported media types should return 415, missing resources should return 404, and completed requests with no content should use 204, not 401.
If a client tries to use the PATCH method on a resource that only supports GET and POST, which status code should the server return?
Explanation: 405 Method Not Allowed tells the client that the method is recognized but not allowed for the requested resource. 200 would incorrectly indicate success, 203 is not relevant in this context, and 501 is used if the method itself is not recognized by the server, which is not the case here.
Which pair of query parameters is most commonly used for pagination in REST APIs?
Explanation: The parameters 'page' and 'limit' are widely used to specify the page number and the number of items per page. 'username' and 'password' are used for authentication. 'filter' and 'sort' may help with querying but are not default pagination parameters. 'accept' and 'content-type' are HTTP headers for content negotiation.
If a PUT request to /users/22 with the same payload is sent twice, what should happen according to idempotency?
Explanation: Idempotency means that applying the same request multiple times results in the same effect as applying it once. PUT updates or creates the user in a specific state, so repeating it does not further change anything. POST creates new resources, which would not be idempotent. Returning errors or deleting users after subsequent identical requests does not align with REST principles.
Which HTTP method is classified as safe, meaning it should not have side effects on the resource?
Explanation: GET is considered safe because it is solely for retrieving data and should not modify any resources on the server. DELETE, PUT, and POST may change server data and are not classified as safe methods.
What does the HTTP status code 429 Too Many Requests indicate when returned from a REST API?
Explanation: 429 Too Many Requests is used by servers to indicate that the user has sent too many requests in a given period, often due to rate limiting. No content to return should use 204. Missing credentials return a 401. Permanent removal would use 410 Gone.
Which condition is best described by the HTTP status code 503 Service Unavailable?
Explanation: 503 indicates the server cannot process the request now, often due to temporary overload or scheduled maintenance. 201 would be used for resource creation. 401 is for authentication issues. Successful deletion would use 204 No Content.
What HTTP method is commonly used for partial updates to an existing resource in a REST API?
Explanation: PATCH is designed for making partial changes to a resource, updating only specified fields. DELETE is for removing resources altogether. TRACE and CONNECT are diagnostic and low-level networking methods, not intended for resource modifications.