REST - jellyfish-tom/TIL GitHub Wiki
[SOURCES]
- https://www.smashingmagazine.com/2016/09/understanding-rest-and-rpc-for-http-apis/
- https://www.ics.uci.edu/~fielding/pubs/dissertation/top.htm
- https://code.tutsplus.com/tutorials/a-beginners-guide-to-http-and-rest--net-16340
REST stands for “representational state transfer,” described by Roy Fielding in his dissertation.
Conforming to the REST constraints is generally referred to as being “RESTful”
REST is all about a client-server relationship, where server-side data are made available through representations of data in simple formats, often JSON and XML. These representations for resources, or collections of resources, which are then potentially modifiable, with actions and relationships being made discoverable via a method known as hypermedia. Hypermedia is fundamental to REST, and is essentially just the concept of providing links to other resources.
Beyond hypermedia there are a few other constraints, such as:
- REST must be stateless: **not persisting sessions between requests (each request is independent from the others, there s no temporary state being persisted between them that they would rely on)
- Responses should declare cacheablility: helps your API scale if clients respect the rules.
- REST focuses on uniformity: if you’re using HTTP you should utilize HTTP features whenever possible, instead of inventing conventions.
REST is not only CRUD, but things are done through mainly CRUD-based operations. REST will use HTTP methods such as GET, POST, PUT, DELETE, OPTIONS, PATCH to provide semantic meaning for the intention of the action being taken.
HTTP verbs
GET
GET instructs the server to transmit the data identified by the URL to the client. Data should never be modified the server side as a result of a GET request. In this sense, a GET request is read-only.
PUT
A PUT request is used when you wish to create or update the resource identified by the URL. PUT modifies entire resource.
Sending a PUT request will update or replace existing data on the server.
Sending a POST request, on the other hand, will create new data on the server, but it will not modify any existing data.
DELETE
DELETE should perform the contrary of PUT; it should be used when you want to delete the resource identified by the URL of the request.
POST
POST is used when the processing you wish to happen on the server should be repeated, if the POST request is repeated (that is, they are not idempotent; more on that below). In addition, POST requests should cause processing of the request body as a subordinate of the URL you are posting to.
In simple words it mostly is used to create entities on the server.
PATCH
PATCH method is a request method supported by the HTTP protocol for making partial changes to an existing resource. The PATCH method provides an entity containing a list of changes to be applied to the resource requested using the HTTP URI. The list of changes are supplied in the form of a PATCH document.
PATCH applies only partial modification to a resource, unlike POST and PUT, which modify the entire resource
OPTIONS
OPTIONS method allows the client to determine the options and/or requirements associated with a resource, or the capabilities of a server, without implying a resource action or initiating a resource retrieval
REST advantages
- REST, both from a client and server's perspective, is REST-based interactions happen using constructs that are familiar to anyone who is accustomed to using the internet's Hypertext Transfer Protocol (HTTP).
- REST is also a language-independent architectural style
- REST is popular so many solutions to support such architecture both in frontend and in backend exists
REST disadvantages
- REST does not preserve user state in backend, so all logic responsible for it needs to be implemented in frontend
- hard to implement PUSH notifications without implementing polling in frontend