1. REST

    & the Web

    Jason Karns

    Consultant, Cardinal Solutions Group

  2. What is REST?

    Representational State Transfer

  3. What is REST?

  4. The WWW is RESTful!

  5. REST vs SOAP?

    Wrong Question!

    ROA vs SOA

    SOA - service-centric

    ROA - resource-centric

    ROA is OOP for the Web

    Now who still wants to program procedurally?

    SOAP is a layer on top of HTTP

    HTTP is RESTful protocol

    RESTful Web Services simply utilize HTTP

    REST is not a protocol

    SOAP can be used over other protocols (SMTP); application or transport layer. HTTP is an application protocol but used as transport protocol by SOAP.

  6. Food for Thought...

    What if the Web was built using XML-RPC, SOAP, or CORBA?

    Barrier to entry would be high. Each site would needs it's own 'client'. Would perhaps have 'richer' data (meta-information) and be closer to Semantic Web. But RDF and microformats are pushing us their now.
  7. Reasons I should be RESTful

    It's Shiny!

    • API usage
    • Performance
      • Caching
      • Thinner Protocol Stack
    • Scalability
    • Thinner clients
      • Thinner Protocol Stack
      • Standard Content-Types
    • Forward/Backward Compatibility
  8. Reasons I should be RESTless

    None

    • Plumbing
    • Strong Typing
    • Contracts
  9. Principles

    The HATEOAS principle basically means your application, as a Finite State Machine, should have all data necessary to change state contained in your response as hyperlinks.

  10. Everything's a Thing

    What is a resource?

  11. ID

    URI/URL

  12. Resources vs Representations

    Media-types

    • http://twitter.com/statuses/jasonkarns.xml
    • http://twitter.com/statuses/jasonkarns.json
    Content Negotiation depends on the following headers:
    • Accept
    • Accept-Language
    • Accept-Encoding
    • Accept-Charset
    • Vary
  13. Operations

    Safe methods should NOT cause any side-effect on the server. Idempotent methods should be repeatable. Any safe method is idempotent but not vice-versa.

    CRUD (POST, GET, PUT, DELETE)

    Tunneling PUT and DELETE over POST: use X-METHOD or _method= parameter

  14. Responses

  15. Caching

  16. Example

    EMS Store

  17. Define Resources

    Products
    http://csg-ems.com/store/products/{product_id}/
    Customers
    http://csg-ems.com/users/{user_id}/
    Shopping Cart
    http://csg-ems.com/users/{user_id}/cart/{product_id}/
    Orders
    http://csg-ems.com/store/orders/{order_id}/
  18. Define Operations

    Products
    List AllGET /store/products/
    View DetailsGET /store/products/{product_id}/
    Add NewPOST /store/products/ or PUT /store/products/{product_id}/
    Update DetailsPUT /store/products/{product_id}/
    RemoveDELETE /store/products/{product_id}/
    Customers
    RegisterPOST /users/ or PUT /users/{user_id}/
    View ProfileGET /users/{user_id}/
    Edit ProfilePUT /users/{user_id}/
    Cart
    Add Item to CartPOST /users/{user_id}/cart/
    Change Item QuantityPUT /users/{user_id}/cart/{product_id}/
    Remove Item from CartDELETE /users/{user_id}/cart/{product_id}/
    Orders
    CheckoutPOST /store/orders/
  19. CSG-EMS

  20. Summary

    REST is awesome!