Try to explain REST

REST is the most popular software architecture on the internet today. In fact, when you read the article about REST, you will find it a little vague.

What is REST

The first REST concepts ( RE presentational S tate T ransfer) were introduced in 2000 in the doctoral dissertation by Roy Thomas Fielding (co-founder of HTTP protocol). In the dissertation he introduces quite a lot of constraints, conventions and how to implement the system to get a REST system.

Simply understanding it is a set of constraints and conventions, when fully applied to your system, we have a REST system.

REST Contraints

  • The system operates according to the client-server model, in which the server is a collection of small services listening to requests from clients. With each different request, one or more services can be processed.
  • Stateless (stateless). Simply, the server and the client do not save each other's status -> each request on the server, the client must encapsulate the information sufficiently for the server to understand. This makes your system easier to develop, maintain, and expand because there is no need to waste the CRUD on the client's state. The system developed in this direction has the advantage but also the disadvantage is to increase the amount of information to be transmitted between the client and server.
  • Caching: The response can be retrieved from the cache. By caching the response, the server reduces the processing of the request, and the client receives the information faster. Here we put a cache in the middle: client-cache-server.
  • Standardizing interfaces: This is one of the important features of REST systems. By creating standard conventions for communication between the components of the system, you have simplified the client's ability to interact with the server. These conventions apply to all services that make it easier for your system users. It is easier to understand on the system you set up an API standard for users whether mobile, web can be connected. The REST system has weaknesses here because when standardized, we cannot optimize each connection.-
  • Classify the system: In the REST system you separate the system components by layer, each class only uses the layer below it and communicates with the class right on it. This helps you reduce the complexity of the system, helping the components separate from each other to easily expand each component:

Học lập trình web online chất lượng

Resources (Resources)

The previous REST system must comply with the above constraints. In detail, the REST system focuses on processing resources. Resource is anything you can call names (a video, photo, website, weather report, etc.). These resources help us define services in the system, the type of information it returns, and its information processing behavior.

Join the Machine Learning event – Technology of the future!

Characteristics describing a resource:

  • Many ways to display: the data you receive can be in many forms (binary, JSON, XML.) This data represents a specific resource.
  • Clear identification: Each URL returns only one specified resource at a time.
  • Description data (metadata): Content type (Content-type), new update, etc.
  • Control data: Is-modifiable-since, cache-control.

The mention of REST refers to HTTP because the REST system often uses the HTTP protocol. The REST system revolves around simplifying taking the representations of a system resource. How?

  • Resource identification: the system needs to provide a solution to identify resources in the system, usually it uses the absolute path to that resource. Each route gives us specific data about specific objects:

This is a solution that does not follow REST because this route points to many different guys over time. With REST, each route is a specific resource

So how do we get the latest book data, please use the query string

There is a lot of debate about not using id in REST, which means that you should not take a numeric value (the id of the data in the database for example) represents a resource because it loses representation for resource. that. For example

Your route is quite clear and if you use ID then

  • Customizing data display: As you know a resource has many ways to display so how can users choose it. There are 2 solutions:
    • Content Negotiation – Insert requests into the header if you use HTTP for example "Accept: text / html;"
    • Use file extension

    Obviously, the second way seems to be easier to use, but it's more difficult to customize than the first.

  • Standardize interaction with resources. We have identified the resource location, we need to interact with it CRUD. Since REST uses HTTP as the system initialization standard, it limits some methods to interact with resources. We use GET to access read data, POST to put new data on the server. PUT to update data and DELETE to delete data. Sometimes POST and PUT are interchangeable. You can evaluate the REST usage level in the system in the following figure:

    alt text

  • Advanced interaction with data. The data you receive needs to be sorted, filtered etc. how to add these interactions when we have finite methods. Please use query string: REST-free route solution GET /api/v1/blogpost/12342 /like GET /api/v1/books /search GET /api/v1/authors /filtering

Clearer with REST

  • Descriptive data: To be able to implement constraints on communication standardization. The REST system's return data requires no information about the guy who returns the data. It complies with a standard installation like HATEOAS (s Hypermedia as the Engine of Application State). Understand simply that your system includes many services to let the client know which one it is interacting with so that the next time it interacts directly with that guy, besides that it also needs to clearly describe the type of data, structure to retrieve data. For example :

This section makes the data you receive easier to understand and makes it easier for the client to have information to continue interacting with. Data metadata can include full description of advanced actions, or support attributes in the system. Since then, the client can easily customize the data. For example

There are structured standards for these guys that you can add to the HAL (Hypertext Application Language). Normally people prefer to set anything, so if the guy who makes the api and the guy who makes the application does not agree, then it is really miserable ….

  • Error handling with status code: As mentioned above, REST has HTTP as the standard protocol. Not always you can always get data and can be status codes like 404 or 500. Always pay attention to write the status code processing logic in REST system.
ITZone via Techmaster
Share the news now