Fundamental principles REST

Asked

Viewed 727 times

4

In this Article by Infoq, the author highlights the five fundamental principles of REST:

  1. Give all things an identifier
  2. Link things (resources/identifier)
  3. Use standardized methods
  4. Features with multiple representations
  5. Communicate stateless

However, one of them would like to help me understand better, it is the principle Use standardized methods. In summary the author says that:

In a Restful HTTP approach, you would have to start with an inteface generic that composed the application’s HTTP protocol. You could do something like that:

inserir a descrição da imagem aqui

My doubt:

  • How would this interface (based on the image)?
  • How would the implementation of a recurso (ID - Identificador) if you want to pass an object json for example

1 answer

3


What this interface would look like (based on the image)?

It depends on your system, but usually a system that implements the REST API (Web API 2 is one of the frameworks that implements the standard) has as interface the support to these four methods (GET, POST, PUT and DELETE). Here is a list of all methods supported by the HTTP protocol. Most of them are not essential. You implement if you want.

The interface is usually defined by the default route + method. In your example, you know what to call /orders for GET, you will get the complete list of entities of type order of the service, and if pass an object serialized by PUT (as a JSON, which is the most common, up to an XML), you are inserting a new entity of type order in the storage of that service.

In the case of Web API 2, documentation on interfaces is automatically generated by framework. Such a resource is known as ApiExplorer.

How would the resource (ID) look if you want to pass a json object, for example?

It depends on each service. If I implement a service accepting that the id will be defined by a request PUT or POST (which is not recommended, but is possible). Most services define the new record id by themselves. When this happens, the new record data is returned in the header of the request response. You can read more about it here (section "Creating a Resource").

Browser other questions tagged

You are not signed in. Login or sign up in order to post.