2. Presentation Itinerary
We’ll learn basics of how to REST stuff.
First we’ll go over definitions that we use for REST.
Then We’ll go over examples of how a RESTful API might be designed.
Finally We’ll look at challenges we run into designing RESTful APIs
3. What is REST?
● REpresentational State Transfer (REST) is an architectural style that defines a
set of constraints and properties based on
● Client – Server architecture
● Uniform Interface
● Statelessness
● Gives client ability to interact with application without prior
knowledge of how to interact.
16. Action – Verbs that do things
● GET – Retrieve a Resource
● POST – Create a new resource
● PUT- Replace a Resource
● PATCH – Edit part of a resource
● DELETE – Remove a resource
● OPTIONS – See what methods can be sent
● HEAD – Peek at a GET Request, no body.
23. Responses
Responses are sent back have a status code, and a message (body).
Status codes are based on standard HTTP status codes.
Body could be in any format, usually but not limited to JSON, XML, HTML.
Body could also include additional instructions for the user to interact with the state
of the object.
24. HTTP Status Codes
● Status Codes gives us a basic idea of how our REQUEST went and what we
should do.
1xx Informational
2xx Success
3xx Redirection
4xx Client Error
5xx Server Error
25.
26. Create a new Wishlist
Status: 201 Created
Location:
27. Adding Items to Wishlist
● Visit the aisle with the item we want. Then we add it to the Wish list
200 OK
ID: 95
28. Adding Item to Wishlist (POST)
ID: 95
QTY: 2
QTY: 2
201 Created
QTY: 2
29. Adding Item to Wishlist (POST)
ID: 5
QTY: 1
QTY: 2
201 Created
QTY: 1QTY: 2 QTY: 1
35. #1. Using GET for state-changing operations
● GET is considered a safe operation.
● Don’t do this: (The Money means purchase)
Instead, maybe do this?
36. #2. Sending wrong Status Code on Response
200 OK
But everything
was not
200 OK
39. #3: Keeping State in the Server
● Server doesn’t know specific state
● State of the application should be on the client.
● Client sends over everything necessary for the
request.
40. #3: Keeping State in the Server: Pagination
● Server should NOT remember what page you’re on.
VS.
41. #4: Having Actions in Resources
REST sometimes is hard since we don’t have enough verbs. So we might do this
42. #4: Having Actions in Resources
1. Try to break out items into better concepts?
Set actions as subresources
i. POST /products/:id/recommendations
ii. POST /layaways/
43. #4: Having Actions in Resources
Some items have state changes that are complex to model.
1. POST /orders/:id/process
2. POST /orders/:id/fulfill
3. POST /orders/:id/hold
4. POST /orders/:id/cancel
44. #4: Having Actions in Resources
● Some concepts just ain’t easy to turn into a noun, or naturally map to a RESTful
Resource
● POST /products/search
45. #4: Having Actions in Resources
● Possible Solution:
Have excellent
documentation
46. #4: Having Actions in Resources
● Possible Solution: Give the client instructions.
47. Hypermedia as the Engine of Application State
(HATEOAS)
● We can list the available commands available for a resource
as part of the RESPONSE.
● This gives no expectations for a client to use.
● Much like how a current website works with links.
50. Areas where REST could be hard
● Bandwidth limited clients (mobile)
○ Look into limiting fields or something like GraphQL
● Designs that have a lot of events and actions
○ Look at RPC style designs.
● You need server to remember state.
● We could be pragmatic, could use multiple designs