Explore your understanding of CRUD operations in RESTful APIs with this quiz covering HTTP methods, resource structures, endpoint conventions, and response codes. Ideal for developers and learners aiming to assess their knowledge of REST API design principles.
Which HTTP method is typically used to create a new resource, such as adding a new user to a user collection in a REST API?
Explanation: POST is the correct method used to create new resources in REST APIs, such as adding an item to a collection. GET is used for retrieving resources, not creating them. PUT is primarily used for updating or completely replacing an existing resource. DELETE, as the name suggests, is for removing resources. Only POST aligns with the standard for resource creation.
When you want to update only the email field of an existing user without changing any other data, which HTTP method should you use to best follow RESTful conventions?
Explanation: PATCH is used for making partial updates to a resource, allowing changes to only specific fields, such as just the email address. PUT replaces the entire resource, so it is less suitable for updating a single field. CREATE and REMOVE are not valid HTTP methods and are incorrect choices. PATCH provides the intended functionality for partial updates.
If you want to retrieve a specific product by its unique identifier using a REST API, which endpoint format is most appropriate?
Explanation: /products/{id} is the standard REST pattern for accessing a single resource by its unique identifier, making it the most appropriate choice. /products/ usually returns the entire collection, not a specific item. /product lacks clarity for collections, and /products?id={id} relies on query parameters instead of clear, resource-based URL paths. Using /products/{id} best adheres to REST guidelines.
Which HTTP status code should a REST API typically return to indicate that a new resource was successfully created?
Explanation: 201 Created is returned when a resource has been successfully created in response to a request, such as POST. 200 OK indicates a general success but is not specific to creation. 400 Bad Request signals an error with the client’s request. 204 No Content is used when an operation was successful but there is no response body, which is common for DELETE operations but not for creation. Therefore, 201 Created is the correct choice.
What should a REST API do when a DELETE request removes an existing resource successfully without returning additional data?
Explanation: A successful DELETE request with no return data should respond with 204 No Content, indicating the deletion was processed properly. 201 Created is only suitable for new resource creation, not deletion. 302 Found is related to redirections rather than deletions. 400 Bad Request signals an issue with the client’s request. 204 No Content clearly tells the client the resource was deleted successfully and there is nothing more to display.