Defining operations via HTTP methods

The operations that can be performed on resources should be defined via HTTP methods. The following are the common methods that are used in a RESTful API:

  • GET method: A GET method retrieves a representation of the resource at the specified URI. The response of the API contains the details of the requested resource. This method should be implemented as a safe operation, meaning that performing a GET operation on resources should not cause any side effects to the state of anything. It can be invoked multiple times and the response of this operation can be cached.
  • POST method: A POST method creates a new resource at the specified URI. The identifier of the resource is generated by the server. The response of the API contains the details of the new resource. A POST method is an unsafe operation, meaning it has side effects. Multiple POST requests will create different resources on the server-side. A POST request can also be used to perform operations other than creating a new resource, for example, moving a card in the TaskAgile application. We will see how that works when we implement that feature.
  • PUT method: A PUT method creates a new resource or updates an existing one. The client specifies the URI for the resource and the request body contains a complete representation of the resource. If a resource with the specified URI already exists, it will be replaced. Otherwise, a new resource is created. When creating a new resource, the difference between using PUT and POST is that the resource identifier in the PUT request is specified by the client. If the server doesn't allow the client to choose the resource identifier, PUT should only be used for updating an existing resource, and when no resource exists at the specified URI, a HTTP 404 response should be returned to the client. A PUT method should be implemented in such a way that it can be performed multiple times with the same effects. This means that a PUT method is idempotent.
  • PATCH method: A PATCH method updates part of an existing resource. The client specifies the URI of the resource, so the request body only contains part of the representation of the resource. The main differences between a PATCH method and a PUT method are that, usually, the request body of a PUT method is a complete representation of the resource, while that of a PATCH method is only a subset of the representation. Another difference is that a PATCH method is not safe nor idempotent.
  • DELETE method: A DELETE method removes an existing resource at the specified URI. DELETE methods are idempotent, meaning that performing a DELETE method on the same resource multiple times indicates that the resource's state is always the same.
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.225.255.178