Upgrade to Pro — share decks privately, control downloads, hide ads and more …

REST my dongle #kreait-dev-days

REST my dongle #kreait-dev-days

REST best practices, HAL, HATEOAS

Armen Mkrtchyan

June 05, 2015
Tweet

More Decks by Armen Mkrtchyan

Other Decks in Programming

Transcript

  1. [ { "id": 1, "content": "Slide 1" }, { "id":

    2, "content": "Slide 2" }, ]
  2. Verb Method GET /slides Get list of slides C POST

    /slides Create new slide R GET /slides/:id Get specific slide U PUT /slides/:id Update slide D DELETE /slides/:id Delete slide
  3. HATEOAS (Hypertext As The Engine Of Application State) Level 3:

    Hypermedia HAL (Hypertext Application Language)
  4. { "id": 5, "content": "Slide 1", "background": "white", "text": "#031337"

    "_links": { "self": { "href": "/slides/5" }, "prev": { "href": "/slides/4" }, "next": { "href": "/slides/6" }, "first": { "href": "/slides/1" }, "last": { "href": "/slides/12" } } }
  5. 2. GET method should NEVER alter the state GET /slide/1?action=disable

    POST /slides/1/disable or PUT /slides/1/disable
  6. 6. Use HAL { "id": 1, "content": "Slide 1", "background":

    "white", "text": "#031337" "_links": { "self": { "href": "/slides/1" }, "next": { "href": "/slides/2" } } }
  7. 7. Filtering, Sorting, field selection and paging Filter GET /slides?background=white

    Sort GET /slides?sort=-title,+length Limit Fields GET /slides?fields=id,title Pagination GET /slides?offset=10&limit=5
  8. 9. Handle Errors with HTTP status codes http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html and https://github.com/joho/7XX-rfc

    Errors in vnd.error format (https://github.com/blongden/vnd.error) or at least something consistent