What are the main advantages of the Restful Api?

Asked

Viewed 203 times

3

Regarding the REST API maturity model, level 2 consists of "HTTP + Resources + Verbs". What are the advantages over level 1 and zero? In addition to organizing the API.

1 answer

2


I will quote some advantages below:

  1. Standardization of the API
    • Division of functionalities by resources
    • Standardization of Uris and parameters
  2. Better use of HTTP protocol resources
    • Convention and use of documentation-based resources (RFC7231 for example)
    • Unifying resource features into a single URL by deriving HTTP methods for registration, change, deletion, etc...
  3. On the last level, we take advantage of HATEOAS as a way to present a resource trail that can be followed with the return of the API
    • With this we can include links that are interesting to the resource, such as links to other pages, to the resource itself, to resources related to the resource, among others
    • It is also supported by documentation (RFC5988 and a draft)
    • The use of links makes it easier for the integrator to have access and navigability to derived resources in a practical and fast way.

Some examples related to the points mentioned above. For the first case, we can apply standardization as follows:

* https://minhaapi.com/getCar?id=2
* https://minhaapi.com/getCustomer?id=5&name=nullptr
* https://minhaapi.com/getManufacturer?id=5

Separando por recursos teremos:

* https://minhaapi.com/cars/{id}
* https://minhaapi.com/customers/{id}?name=nullptr
* https://minhaapi.com/manufacturers/{id}

Still taking advantage of the hook for item 2, with the best use of resources we will have the following scenario:

* GET  https://minhaapi.com/getCars
* GET  https://minhaapi.com/getCar?id=2
* POST https://minhaapi.com/createCar
* POST https://minhaapi.com/updateCar
* POST https://minhaapi.com/deleteCar

Utilizando os verbos HTTP

* POST      https://minhaapi.com/cars
* GET       https://minhaapi.com/cars
* GET       https://minhaapi.com/cars/{id}
* PUT/PATCH https://minhaapi.com/cars/{id}
* DELETE    https://minhaapi.com/cars/{id}

And now applying HATEOAS in the return of resources, we would have the possibility to implement navigability to our content of Sponse:

* GET  https://minhaapi.com/cars/5

HTTP 200

{
    "car": {
        "id": "5"
        "model": "Fusca",
        "manufactor": "VW"
    },
    "links": {
        "_self": { "href": "https://minhaapi.com/cars/5" }
        "manufacturer": { "href": "https://minhaapi.com/manufactors/56" }
        "similar": { "href": "https://minhaapi.com/cars?related=fusca" }
    }
}

You can also include links to help with paging, when a listing endpoint query is paginated, an example can be seen in this call of the Juno API.

Browser other questions tagged

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