Web Technology Basics
Which of the following is an appropriate web API design that follows the principles of REST?
- APut the operation name into the URL, as in /getUser?id=42 or /deleteUser?id=42, and send everything with GET
- BRepresent a user resource with a URL such as /users/42 and express operations with HTTP methods, such as GET to retrieve and DELETE to delete
- CHave the server remember each client's earlier requests, so that the next request only sends a short command that depends on them
Show answer and explanation
Answer: B. Represent a user resource with a URL such as /users/42 and express operations with HTTP methods, such as GET to retrieve and DELETE to delete
In REST, the things being handled are represented as resources identified by URLs, and operations on them are expressed with HTTP methods such as GET (retrieve), POST (create), PUT or PATCH (update) and DELETE (delete). For example, GET /users/42 retrieves a resource, and when POST /users creates a new user, the server returns 201 Created with the URL of the new resource. Each request is made stateless, containing all the information needed to process it. If an operation such as deletion is done with GET, data may be lost merely because a browser fetches links in advance or a crawler visits.
- A. Putting operation names into URLs differs from the REST approach. Also, deleting with GET risks unintended execution, for example when links are fetched in advance.
- B. Correct. Resources are expressed with URLs and operations with HTTP methods.
- C. In REST each request is independent, and the design is stateless so the server does not rely on earlier requests.