Efficient-web-app-architecture
GitHubToggle Dark/Light/Auto modeToggle Dark/Light/Auto modeToggle Dark/Light/Auto modeBack to homepage

Operations

Define operations in terms of HTTP methods

The HTTP protocol defines a number of methods that assign semantic meaning to a request. The common HTTP methods used by most RESTful web APIs are:

  • GET retrieves a representation of the resource at the specified URI. The body of the response message contains the details of the requested resource.
  • POST creates a new resource at the specified URI. The body of the request message provides the details of the new resource. Note that POST can also be used to trigger operations that don’t actually create resources.
  • PUT either creates or replaces the resource at the specified URI. The body of the request message specifies the resource to be created or updated.
  • PATCH performs a partial update of a resource. The request body specifies the set of changes to apply to the resource.
  • DELETE removes the resource at the specified URI.

The effect of a specific request should depend on whether the resource is a collection or an individual item. The following table summarizes the common conventions adopted by most RESTful implementations using the e-commerce example.

ResourcePOSTGETPUTDELETE
/customersCreate a new customerRetrieve all customersBulk update of customersRemove all customers
/customers/1ErrorRetrieve the details for customer 1Update the details of customer 1 if it existsRemove customer 1
/customers/1/ordersCreate a new order for customer 1Retrieve all orders for customer 1Bulk update of orders for customer 1Remove all orders for customer 1
/orders/2ErrorRetrieve the details for order 2Update the details of order 2Remove order 2

Another example (books):

ResourcePOSTGETPUTDELETE
/booksCreate a new booksRetrieve all booksBulk update of booksRemove all books
/books/1ErrorRetrieve the details for book 1Update the details of book 1 if it existsRemove book 1
/books/1/commentsCreate a new comment for book 1Retrieve all comments for book 1Bulk update of comments for book 1Remove all comments for book 1