SlideShare ist ein Scribd-Unternehmen logo
1 von 72
Representational
State
Transfer
(REST)
(and some other important stuff)
Warning before we start



  ‣ REST != MVC
  ‣ Do not think in controllers, id’s,
    actions, models, views, plugins,
    helpers etc...
  ‣ In fact, do not think about
    implementation AT ALL!!
What is REST?



Roy Fielding said:
  REST is a coordinated set of architectural constraints that attempts to minimize
  latency and network communication while at the same time maximizing the
  independence and scalability of component implementations. This is achieved by
  placing constraints on connector semantics where other styles have focused on
  component semantics. REST enables the caching and reuse of interactions,
  dynamic substitutability of components, and processing of actions by
  intermediaries, thereby meeting the needs of an Internet-scale distributed
  hypermedia system.
What is REST?



    ‣ Cacheable
    ‣ Stateless
    ‣ Scalable
    ‣ Fault-tolerant
    ‣ Loosely coupled


‣ ADVANTAGES OF REST
What is REST?




     ‣   URL identifies a resource
     ‣   URLs have an hierarchy
     ‣   Methods perform operations on resources
     ‣   Operation must be implicit
     ‣   Hypermedia format to represent data
     ‣   Link relations to navigate



‣ THE PRINCIPLES OF REST
What is REST?




     ‣ Identification of resources
     ‣ Manipulation of resources
     ‣ Self-descriptive messages
     ‣ HATEOAS



‣ THE FOUR MAIN PRINCIPLES
What is REST?




     ‣ Identification of resources
     ‣ Manipulation of resources
     ‣ Self-descriptive messages
     ‣ HATEOAS



‣ THE FOUR MAIN PRINCIPLES
Identification of resources




     ‣ /index.php?action=getarticle&id=5
     ‣ /default/article/5/4/6/size




           Cacheable?            Scalable?   Readable?

‣ You are doing it wrong... :(
Identification of resources



     ‣ /articles
      We want all articles


     ‣ /articles/5/photos/4/comments/1
      We want the first comment of the fourth photo for the fifth article


     ‣ /articles/5/photos/4/comments
     We want all comments of the fourth photo for the fifth article


           Cacheable!          Scalable!        Readable!

‣ Readable and maintainable!
Identification of resources




      ✗ /photos/order/size/limit/5
      ✗ /photos/limit/5/order/size
      ✓ /photos?order=size&limit=5
      ✓ /photos?limit=5&order=size

‣ FILTERING THROUGH A QUERY STRING, NOT THE URI
What is REST?




     ‣ Identification of resources
     ‣ Manipulation of resources
     ‣ Self-descriptive messages
     ‣ HATEOAS



‣ THE FOUR MAIN PRINCIPLES
Manipulation of resources



  ‣ Create
  ‣ Retrieve
  ‣ Update
  ‣ Delete
  ‣ But please note that REST != CRUD
Manipulation of resources




     ‣ Create = PUT
     ‣ Retrieve = GET
     ‣ Update = POST
     ‣ Delete = DELETE



‣ CRUD to HTTP verb mapping
Manipulation of resources




    ‣ Any client should be able to make
      the request as many times as
      necessary.
    ‣ GET, OPTIONS, HEAD



‣ SAFE METHODS
Manipulation of resources




    ‣ Garantuees that the client can
      repeat the request when it’s not
      certain.
    ‣ $x++ vs $x=4
    ‣ ALL METHODS EXCEPT “POST”


‣ IDEMPOTENT METHODS
What is REST?




     ‣ Identification of resources
     ‣ Manipulation of resources
     ‣ Self-descriptive messages
     ‣ HATEOAS



‣ THE FOUR MAIN PRINCIPLES
Self-descriptive messages




‣ Stateless!
‣ All information for processing is available:
 ‣   How? (method + content-type)
 ‣   What? (URI)
 ‣   When? (preconditions)
 ‣   Who? (authentication)
Self-descriptive messages



            GET /article/1234 HTTP/1.1
            Host: www.enrise.com
            Accept: application/vnd.enrise.nl+xml ; version: 1.0
            Authorization: OAuth oauth_nonce=”123” ...
            If-None-Matched: absad12412414




‣ HOW (method)
Self-descriptive messages



             GET /article/1234 HTTP/1.1
             Host: www.enrise.com
             Accept: application/vnd.enrise.nl+xml ; version: 1.0
             Authorization: OAuth oauth_nonce=”123” ...
             If-None-Matched: absad12412414




‣ HOW (content-type)
Self-descriptive messages



          GET /article/1234 HTTP/1.1
          Host: www.enrise.com
          Accept: application/vnd.enrise.nl+xml ; version: 1.0
          Authorization: OAuth oauth_nonce=”123” ...
          If-None-Matched: absad12412414




‣ WHAT
Self-descriptive messages



          GET /article/1234 HTTP/1.1
          Host: www.enrise.com
          Accept: application/vnd.enrise.nl+xml ; version: 1.0
          Authorization: OAuth oauth_nonce=”123” ...
          If-None-Matched: absad12412414




‣ WHEN
Self-descriptive messages



          GET /article/1234 HTTP/1.1
          Host: www.enrise.com
          Accept: application/vnd.enrise.nl+xml ; version: 1.0
          Authorization: OAuth oauth_nonce=”123” ...
          If-None-Matched: absad12412414




‣ WHO
What is REST?




     ‣ Identification of resources
     ‣ Manipulation of resources
     ‣ Self-descriptive messages
     ‣ HATEOAS



‣ THE FOUR MAIN PRINCIPLES
HATEOAS




                 HATEOAS
                     =
 Hypermedia As The Engine Of Application State
HATEOAS




           This is the hardest and
   of course, most important part of REST
HATEOAS



  ‣ Use links to allow clients to discover
    locations and operations.
  ‣ Link relations are used to express
    options.
  ‣ Clients do not need to know URLs.
  ‣ This controls the state.
State inside your REST API




                                          Fetch
          Select         Confirm   Pay
                                         E-Ticket




‣ “Flight booking API”
State inside your REST API



               POST /search?order=price&limit=5 HTTP/1.1
               Host: www.enrise.com
               Accept: application/vnd.enrise.nl+xml ; version: 1.0

               <search>
                 <destination>LPA</destination>
                 <date>24-may-2011</date>
                 <type>firstclass</type>
               </search>




‣ Search for specified flights
State inside your REST API



        HTTP/1.1 200 OK
        Content-type: application/vnd.enrise.nl+xml ; version: 1.0

        <flights xmlns=...>
          <flight>
            <flightno>KL1234</flightno>
            <time>4:24</time>
            <link method=”get” rel=”details” action=”/flight/15263” type=”text/xml”>
            <link method=”post” rel=”confirm” action=”/confirm/flight/15263” type=”text/xml”>
          </flight>
         <flight>
            <flightno>HV123</flightno>
            <time>3:54</time>
            <link method=”get” rel=”details” action=”/flight/523525” type=”text/xml”>
            <link method=”post” rel=”confirm” action=”/confirm/flight/523525” type=”text/xml”>
          </flight>
        </flights>




‣ Returns a collection of flights
State inside your REST API


          POST /confirm/flight/15263 HTTP/1.1
          Host: www.enrise.com
          Accept: application/vnd.enrise.nl+xml ; version: 1.0

          <flight>
            <seat>2A</seat>
            <meal>vegetarian</meal>
          </flight>




          HTTP/1.1 401 Authentication required




‣ Confirm a specific flight
State inside your REST API


          POST /confirm/flight/15263 HTTP/1.1
          Host: www.enrise.com
          Accept: application/vnd.enrise.nl+xml ; version: 1.0
          Authorization: OAuth ......

          <flight>
            <seat>2A</seat>
            <meal>vegetarian</meal>
          </flight>



          HTTP/1.1 200 OK
          Location: /booking/1616163

          <booking>
            <link rel=”details” href=”/booking/1616163” method=”get” type=”application/xml”>
            <link rel=”payment” href=”/payment/booking/1616163” type=”application/xml”>
            <link rel=”cancel” href=”/booking/1616163” method=”delete” type=”application/xml”>
          </booking>




‣ Confirm a specific flight, with more info
State inside your REST API




          OPTIONS /booking/1616163 HTTP/1.1
          Host: www.enrise.com
          Authorization: OAuth ......




          HTTP/1.1 200 OK
          Allow: GET, DELETE, PUT




‣ What can we do with our booking?
State inside your REST API




          DELETE /booking/1616163 HTTP/1.1
          Host: www.enrise.com
          Authorization: OAuth ......




          HTTP/1.1 204 No content




‣ Cancel our booking!
State inside your REST API



           GET /booking/1616163 HTTP/1.1
           Host: www.enrise.com
           Accept: application/vnd.enrise.nl+xml ; version: 1.0
           Authorization: OAuth...

           HTTP/1.1 200 OK

           <booking>
             <flight>
                <flightno>KL1234</flightno>
                <time>4:24</time>
                <link rel=”details” method=”get” href=”/flight/15263”>
             </flight>
             <payment>
               <status>Not paid</status>
               <link rel=”details” method=”get” href=”/payment/booking/1616163”>
               <link rel=”pay” method=”post” href=”/payment/booking/1616163”>
             </payment>
           </booking>




‣ Still need to pay for the flight
State inside your REST API


          PUT /payment/booking/1616163 HTTP/1.1
          Host: www.enrise.com
          Accept: application/vnd.enrise.nl+xml ; version: 1.0
          Authorization: OAuth ......

          <payment>
            <cardno>4111-1111-1111-1111</cardno>
            <expires>04/13</expires>
            <name>Joshua Thijssen</name>
            <amount currency=”eur”>414.00</amount>
          </payment>




          HTTP/1.1 201 Created
          Location: /payment/booking/1616163




‣ Pay through another resource
State inside your REST API




           OPTIONS /booking/1616163 HTTP/1.1
           Host: www.enrise.com
           Authorization: OAuth ......




           HTTP/1.1 200 OK
           Allow: GET




‣ Can’t delete our booking since it’s paid
State inside your REST API



          GET /booking/1616163 HTTP/1.1
          Host: www.enrise.com
          Accept: application/vnd.enrise.nl+xml ; version: 1.0
          Authorization: OAuth...

          HTTP/1.1 200 OK

          <booking>
            <flight>
               <flightno>KL1234</flightno>
               <time>4:24</time>
               <link rel=”details” method=”get” href=”/flight/15263”>
            </flight>
            <payment>
              <status>Paid in full</status>
              <link rel=”details” method=”get” href=”/payment/booking/1616163”>
            </payment>
            <link rel=”eticket” method=”get” href=”/eticket/12415156261616”>
          </booking>




‣ We can fetch our eticket now
(Common) pitfalls of REST design




 ‣ Versioning
 ‣ Methods in uri
 ‣ One uri per resource
 ‣ Controller resources
   & non-CRUD
(Common) pitfalls of REST design




 ‣ Versioning
 ‣ Methods in uri
 ‣ One uri per resource
 ‣ Controller resources & Non-CRUD
(Common) pitfalls of REST design




   ‣ /api/v1.1/article/1234/photos
   ‣ /api/v1.2/article/1234/photos
   ‣ Different resources?




‣ Versioning
(Common) pitfalls of REST design




               GET /api/article/1234/photos HTTP/1.1
               Host: www.enrise.com
               Accept: application/vnd.enrise.nl+xml ; version = 1.0




               GET /api/article/1234/photos HTTP/1.1
               Host: www.enrise.com
               Accept: application/vnd.enrise.nl+json ; version = 1.1




               GET /api/article/1234/photos HTTP/1.1
               Host: www.enrise.com
               Accept: application/vnd.enrise.nl+json ; version = 5.0.4a




‣ Versioning
(Common) pitfalls of REST design




 ‣ Versioning
 ‣ Methods in uri
 ‣ One uri per resource
 ‣ Controller resources & Non-CRUD
(Common) pitfalls of REST design




   ‣ /api/get/articles/1234/photos
   ‣ /api/articles/new
   ‣ /api/articles/list




‣ Methods in URL
(Common) pitfalls of REST design




 ‣ Versioning
 ‣ Methods in uri
 ‣ One uri per resource
 ‣ Controller resources & Non-CRUD
(Common) pitfalls of REST design




   ‣ /api/article/1234
   ‣ /api/article/red+teddybear
   ‣ Different resources




‣ One URI per resource
(Common) pitfalls of REST design




           GET /api/article/red+teddybear HTTP/1.1
           Host: www.enrise.com
           Accept: application/vnd.enrise.nl+xml ; version = 1.0




           HTTP/1.1 303 See Other
           Location: /api/article/1234




‣ One URI per resource
(Common) pitfalls of REST design




 ‣ Versioning
 ‣ Methods in uri
 ‣ One uri per resource
 ‣ Controller resources & Non-CRUD
(Common) pitfalls of REST design




   ‣ Outside the CRUD?
   ‣ Multiple operations simultaneously?




‣ Controller resources & non-crud
(Common) pitfalls of REST design



   POST /distance HTTP/1.1                                 GET /distance?from=...&to=.. HTTP/1.1
   Host: www.enrise.com                                    Host: www.enrise.com
   Accept: text/xml                                        Accept: text/xml
   Content-type: text/xml;charset=UTF-8

   <xml>
     <from>Amersfoort, NLD</from>
     <to>Apeldoorn, NLD</to>
   </xml>



               HTTP/1.1 200 OK
               Content-length: 123
               Content-type: text/xml

               <result>
                 <distance unit=”km”>45</distance>
                 <distance unit=”miles”>27.96</distance>
               </result>




‣ Controller resources & non-crud
(Common) pitfalls of REST design



           POST /user/jthijssen/address_merge HTTP/1.1
           Host: www.enrise.com
           Accept: application/vnd.enrise.nl+xml ; version = 1.0
           Content-type: text/csv;charset=UTF-8

           John Doe, 1 Main Street, Seattle, WA
           Jane Doe, 100 North Street, Los Angeles, CA




           HTTP/1.1 303 See Other
           Location: /user/jthijssen/addressbook




‣ Controller resources & non-crud
More important stuff




  ‣ HTTP Status codes
  ‣ ETags
HTTP Status codes




 ‣ Status codes are important
 ‣ They represent the result of your
   actions
HTTP Status codes



         ‣ 1xx      Informational
         ‣ 2xx      Success
         ‣ 3xx      Redirection
         ‣ 4xx      Client error
         ‣ 5xx      Server error
HTTP Status codes




   ‣ 200 OK
     Resource returned

   ‣ 201 Created
     Resource created

   ‣ 204 No content
     Resource deleted

‣ IMPORTANT 2xx CODES
HTTP Status codes




   ‣ 304 Not modified
     Resource wasn’t changed




‣ IMPORTANT 3xx CODES
HTTP Status codes



   ‣ 400 Bad request
     Incorrect payload
   ‣ 401 Unauthorized
     Not authorized to operate
   ‣ 403 Forbidden
     Not authorized to operate
   ‣ 404 Not found
     Resource was not found

‣ IMPORTANT 4xx CODES
HTTP Status codes



    ‣ 405 Method not allowed
      Method incorrect
    ‣ 406 Not acceptable
      Cannot return in correct format
    ‣ 412 Precondition failed
      “ETag mismatch”



‣ IMPORTANT 4xx CODES
HTTP Status codes




   ‣ 500 Internal server error
     “Something” happened
   ‣ 501 Not implemented
     Method is not implemented



‣ IMPORTANT 5xx CODES
HTTP Status codes




   ‣ 501 Not implemented vs
     405 Method not allowed


   ‣ 409 Conflict vs
     412 Precondition failed



‣ de·bat·a·ble/diˈbātəbəl/Adjective
ETags & Optimistic locking



        GET /blogpost/12345 HTTP/1.1
        Host: www.enrise.com




        HTTP/1.1 200 OK
        Content-length: 1234000
        Content-type: text/xml
        ETag: abcd-1234

        <xml>
        <blogpost>
          <author>Joshua Thijssen</author>
          <title>...</title>
          ....
        </blogpost>
        </xml>
ETags & Optimistic locking


        GET /blogpost/12345 HTTP/1.1
        Host: www.enrise.com
        If-None-Match: abcd-1234




        HTTP/1.1 304 Not modified




     Blogpost is cached and can be used!
ETags & Optimistic locking


        POST /blogpost/12345 HTTP/1.1
        Host: www.enrise.com
        If-Match: abcd-1234

        <xml>
          <author>Sjors de Valk</author>
        </xml>



        HTTP/1.1 412 Precondition failed




  Blogpost is already modified by “someone”
REST examples




            Other REST Examples
REST examples


           PUT /articles HTTP/1.1
           Host: www.enrise.com
           Content-type: application/vnd.enrise.nl+xml ; version = 1.0

           <article xmlns=”http://www.enrise.com/article”>
             <name>Teddybear</name>
             <color>red</color>
             <stock>15</stock>
             <price currency=”eur”>15,95</price>
             <price currency=”usd”>19,95</price>
           </article>




           HTTP/1.1 201 Created
           Location: /articles/1234




‣ Creating a resource
REST examples



           GET /articles HTTP/1.1
           Host: www.enrise.com
           Accept: application/vnd.enrise.nl+xml ; version = 1.0


           HTTP/1.1 200 OK
           Content-length: 12345
           Content-type: application/vnd.enrise.nl+xml
           Date: sun, 01 Nov 2010 12:34:56 GMT

           <articles xmlns=”http://www.enrise.com/article”>
             <article>
               <name>Teddybear</name>
               <link method=”get” rel=”article” href=”/articles/1234”>
             </article>
             <article>
               <name>Skippyball</name>
               <link method=”get” rel=”article” href=”/articles/1121”>
             </article>
           </articles>




‣ Getting a resource collection
REST examples



           GET /articles/1234 HTTP/1.1
           Host: www.enrise.com
           Accept: application/vnd.enrise.nl+xml ; version = 1.0



           HTTP/1.1 200 OK
           Content-length: 12345
           Content-type: application/vnd.enrise.nl+xml
           Date: sun, 01 Nov 2010 12:34:56 GMT

           <article xmlns=”http://www.enrise.com/article”>
             <name>Teddybear</name>
             ...
           </article>


           HTTP/1.1 404 Not found
           Content-length: 0
           Date: sun, 01 Nov 2010 12:34:56 GMT




‣ Getting a resource
REST examples



           DELETE /articles/1234 HTTP/1.1
           Host: www.enrise.com
           Accept: application/vnd.enrise.nl+xml ; version = 1.0




           HTTP/1.1 204 No content
           Content-length: 0
           Date: sun, 01 Nov 2010 12:34:56 GMT




‣ Delete a resource
REST examples

          POST /articles/1234 HTTP/1.1
          Host: www.enrise.com
          Accept: application/vnd.enrise.nl+xml ; version = 1.0   Idempotent
          If-Match: 23709-12135125

          <article xmlns=”http://www.enrise.com/article>
            <name>Teddybear</name>
            <color>red</color>
            <stock>30</stock>
            <price currency=”eur”>15,95</price>
            <price currency=”usd”>19,95</price>
          </article>



          HTTP/1.1 200 OK
          Content-length: 0
          Date: sun, 01 Nov 2010 12:34:56 GMT


          HTTP/1.1 412 Precondition failed
          Content-length: 0
          Date: sun, 01 Nov 2010 12:34:56 GMT




‣ Updating a resource
(Common) pitfalls of REST design



           POST /user/jthijssen/address_merge HTTP/1.1
           Host: www.enrise.com
           Accept: application/vnd.enrise.nl+xml ; version = 1.0
           Content-type: text/csv;charset=UTF-8

           John Doe, 1 Main Street, Seattle, WA
           Jane Doe, 100 North Street, Los Angeles, CA




           HTTP/1.1 303 See Other
           Location: /user/jthijssen/addressbook




‣ Controller resources
More reading



‣    http://en.wikipedia.org/wiki/Representational_State_Transfer

‣    http://www.ics.uci.edu/~fielding/pubs/dissertation/top.htm

‣    http://oreilly.com/catalog/9780596801694

‣    http://www.slideshare.net/Wombert/designing-http-interfaces-and-
     restful-web-services-confoo11-20110310

‣    http://www.slideshare.net/adorepump/hateoas-the-confusing-bit-
     from-rest

‣    http://www.slideshare.net/guilhermecaelum/rest-in-practice
Question & discussion




http://farm1.static.flickr.com/73/163450213_18478d3aa6_d.jpg
‣ THANK YOU FOR YOUR ATTENTION

Weitere ähnliche Inhalte

Andere mochten auch

Puppet for dummies - PHPBenelux UG edition
Puppet for dummies - PHPBenelux UG editionPuppet for dummies - PHPBenelux UG edition
Puppet for dummies - PHPBenelux UG editionJoshua Thijssen
 
Deploying and maintaining your software with RPM/APT
Deploying and maintaining your software with RPM/APTDeploying and maintaining your software with RPM/APT
Deploying and maintaining your software with RPM/APTJoshua Thijssen
 
Alice & bob public key cryptography 101 - uncon dpc
Alice & bob  public key cryptography 101 - uncon dpcAlice & bob  public key cryptography 101 - uncon dpc
Alice & bob public key cryptography 101 - uncon dpcJoshua Thijssen
 
15 protips for mysql users
15 protips for mysql users15 protips for mysql users
15 protips for mysql usersJoshua Thijssen
 
PFZ WorkshopDay Linux - Advanced
PFZ WorkshopDay Linux - AdvancedPFZ WorkshopDay Linux - Advanced
PFZ WorkshopDay Linux - AdvancedJoshua Thijssen
 
PFZ WorkshopDay Linux - Basic
PFZ WorkshopDay Linux - BasicPFZ WorkshopDay Linux - Basic
PFZ WorkshopDay Linux - BasicJoshua Thijssen
 
15 protips for mysql users pfz
15 protips for mysql users   pfz15 protips for mysql users   pfz
15 protips for mysql users pfzJoshua Thijssen
 
Alice & bob public key cryptography 101
Alice & bob  public key cryptography 101Alice & bob  public key cryptography 101
Alice & bob public key cryptography 101Joshua Thijssen
 
Alice & bob public key cryptography 101
Alice & bob  public key cryptography 101Alice & bob  public key cryptography 101
Alice & bob public key cryptography 101Joshua Thijssen
 
Puppet for dummies - ZendCon 2011 Edition
Puppet for dummies - ZendCon 2011 EditionPuppet for dummies - ZendCon 2011 Edition
Puppet for dummies - ZendCon 2011 EditionJoshua Thijssen
 
Unix command-line tools
Unix command-line toolsUnix command-line tools
Unix command-line toolsEric Wilson
 
Sed & awk the dynamic duo
Sed & awk   the dynamic duoSed & awk   the dynamic duo
Sed & awk the dynamic duoJoshua Thijssen
 

Andere mochten auch (18)

Moved 301
Moved 301Moved 301
Moved 301
 
Puppet for dummies - PHPBenelux UG edition
Puppet for dummies - PHPBenelux UG editionPuppet for dummies - PHPBenelux UG edition
Puppet for dummies - PHPBenelux UG edition
 
Workshop unittesting
Workshop unittestingWorkshop unittesting
Workshop unittesting
 
Deploying and maintaining your software with RPM/APT
Deploying and maintaining your software with RPM/APTDeploying and maintaining your software with RPM/APT
Deploying and maintaining your software with RPM/APT
 
Alice & bob public key cryptography 101 - uncon dpc
Alice & bob  public key cryptography 101 - uncon dpcAlice & bob  public key cryptography 101 - uncon dpc
Alice & bob public key cryptography 101 - uncon dpc
 
15 protips for mysql users
15 protips for mysql users15 protips for mysql users
15 protips for mysql users
 
PFZ WorkshopDay Linux - Advanced
PFZ WorkshopDay Linux - AdvancedPFZ WorkshopDay Linux - Advanced
PFZ WorkshopDay Linux - Advanced
 
PFZ WorkshopDay Linux - Basic
PFZ WorkshopDay Linux - BasicPFZ WorkshopDay Linux - Basic
PFZ WorkshopDay Linux - Basic
 
15 protips for mysql users pfz
15 protips for mysql users   pfz15 protips for mysql users   pfz
15 protips for mysql users pfz
 
Alice & bob public key cryptography 101
Alice & bob  public key cryptography 101Alice & bob  public key cryptography 101
Alice & bob public key cryptography 101
 
Cipher block modes
Cipher block modesCipher block modes
Cipher block modes
 
Awk programming
Awk programming Awk programming
Awk programming
 
Czzawk
CzzawkCzzawk
Czzawk
 
Alice & bob public key cryptography 101
Alice & bob  public key cryptography 101Alice & bob  public key cryptography 101
Alice & bob public key cryptography 101
 
Naive Bayes
Naive Bayes Naive Bayes
Naive Bayes
 
Puppet for dummies - ZendCon 2011 Edition
Puppet for dummies - ZendCon 2011 EditionPuppet for dummies - ZendCon 2011 Edition
Puppet for dummies - ZendCon 2011 Edition
 
Unix command-line tools
Unix command-line toolsUnix command-line tools
Unix command-line tools
 
Sed & awk the dynamic duo
Sed & awk   the dynamic duoSed & awk   the dynamic duo
Sed & awk the dynamic duo
 

Ähnlich wie Representation state transfer and some other important stuff

RESTful services
RESTful servicesRESTful services
RESTful servicesgouthamrv
 
Introduction to REST and Hypermedia
Introduction to REST and HypermediaIntroduction to REST and Hypermedia
Introduction to REST and HypermediaNordic APIs
 
How to build Simple yet powerful API.pptx
How to build Simple yet powerful API.pptxHow to build Simple yet powerful API.pptx
How to build Simple yet powerful API.pptxChanna Ly
 
REST in ( a mobile ) peace @ WHYMCA 05-21-2011
REST in ( a mobile ) peace @ WHYMCA 05-21-2011REST in ( a mobile ) peace @ WHYMCA 05-21-2011
REST in ( a mobile ) peace @ WHYMCA 05-21-2011Alessandro Nadalin
 
I got 99 problems, but ReST ain't one
I got 99 problems, but ReST ain't oneI got 99 problems, but ReST ain't one
I got 99 problems, but ReST ain't oneAdrian Cole
 
Architecting &Building Scalable Secure Web API
Architecting &Building Scalable Secure Web APIArchitecting &Building Scalable Secure Web API
Architecting &Building Scalable Secure Web APISHAKIL AKHTAR
 
RefCard RESTful API Design
RefCard RESTful API DesignRefCard RESTful API Design
RefCard RESTful API DesignOCTO Technology
 
The REST And Then Some
The REST And Then SomeThe REST And Then Some
The REST And Then SomeNordic APIs
 
The dark side of the app - Todi Appy Days 2015
The dark side of the app - Todi Appy Days 2015The dark side of the app - Todi Appy Days 2015
The dark side of the app - Todi Appy Days 2015Todi Appy Days
 
Design Web Service API by HungerStation
Design Web Service API by HungerStationDesign Web Service API by HungerStation
Design Web Service API by HungerStationArabNet ME
 
Shields Up! Securing React Apps
Shields Up! Securing React AppsShields Up! Securing React Apps
Shields Up! Securing React AppsZachary Klein
 
Learn REST in 18 Slides
Learn REST in 18 SlidesLearn REST in 18 Slides
Learn REST in 18 SlidesSuraj Gupta
 
distributing over the web
distributing over the webdistributing over the web
distributing over the webNicola Baldi
 
HTTP fundamentals for developers
HTTP fundamentals for developersHTTP fundamentals for developers
HTTP fundamentals for developersMario Cardinal
 
API Best Practices
API Best PracticesAPI Best Practices
API Best PracticesUri Lavi
 

Ähnlich wie Representation state transfer and some other important stuff (20)

Doing REST Right
Doing REST RightDoing REST Right
Doing REST Right
 
RESTful Web Services
RESTful Web ServicesRESTful Web Services
RESTful Web Services
 
RESTful services
RESTful servicesRESTful services
RESTful services
 
Introduction to REST and Hypermedia
Introduction to REST and HypermediaIntroduction to REST and Hypermedia
Introduction to REST and Hypermedia
 
How to build Simple yet powerful API.pptx
How to build Simple yet powerful API.pptxHow to build Simple yet powerful API.pptx
How to build Simple yet powerful API.pptx
 
REST in ( a mobile ) peace @ WHYMCA 05-21-2011
REST in ( a mobile ) peace @ WHYMCA 05-21-2011REST in ( a mobile ) peace @ WHYMCA 05-21-2011
REST in ( a mobile ) peace @ WHYMCA 05-21-2011
 
REST
RESTREST
REST
 
I got 99 problems, but ReST ain't one
I got 99 problems, but ReST ain't oneI got 99 problems, but ReST ain't one
I got 99 problems, but ReST ain't one
 
Architecting &Building Scalable Secure Web API
Architecting &Building Scalable Secure Web APIArchitecting &Building Scalable Secure Web API
Architecting &Building Scalable Secure Web API
 
RefCard RESTful API Design
RefCard RESTful API DesignRefCard RESTful API Design
RefCard RESTful API Design
 
HTTP Basics Demo
HTTP Basics DemoHTTP Basics Demo
HTTP Basics Demo
 
The REST And Then Some
The REST And Then SomeThe REST And Then Some
The REST And Then Some
 
The dark side of the app - Todi Appy Days 2015
The dark side of the app - Todi Appy Days 2015The dark side of the app - Todi Appy Days 2015
The dark side of the app - Todi Appy Days 2015
 
Design Web Service API by HungerStation
Design Web Service API by HungerStationDesign Web Service API by HungerStation
Design Web Service API by HungerStation
 
RESTful Web Services
RESTful Web ServicesRESTful Web Services
RESTful Web Services
 
Shields Up! Securing React Apps
Shields Up! Securing React AppsShields Up! Securing React Apps
Shields Up! Securing React Apps
 
Learn REST in 18 Slides
Learn REST in 18 SlidesLearn REST in 18 Slides
Learn REST in 18 Slides
 
distributing over the web
distributing over the webdistributing over the web
distributing over the web
 
HTTP fundamentals for developers
HTTP fundamentals for developersHTTP fundamentals for developers
HTTP fundamentals for developers
 
API Best Practices
API Best PracticesAPI Best Practices
API Best Practices
 

Kürzlich hochgeladen

Are Multi-Cloud and Serverless Good or Bad?
Are Multi-Cloud and Serverless Good or Bad?Are Multi-Cloud and Serverless Good or Bad?
Are Multi-Cloud and Serverless Good or Bad?Mattias Andersson
 
The Ultimate Guide to Choosing WordPress Pros and Cons
The Ultimate Guide to Choosing WordPress Pros and ConsThe Ultimate Guide to Choosing WordPress Pros and Cons
The Ultimate Guide to Choosing WordPress Pros and ConsPixlogix Infotech
 
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)Mark Simos
 
Moving Beyond Passwords: FIDO Paris Seminar.pdf
Moving Beyond Passwords: FIDO Paris Seminar.pdfMoving Beyond Passwords: FIDO Paris Seminar.pdf
Moving Beyond Passwords: FIDO Paris Seminar.pdfLoriGlavin3
 
Nell’iperspazio con Rocket: il Framework Web di Rust!
Nell’iperspazio con Rocket: il Framework Web di Rust!Nell’iperspazio con Rocket: il Framework Web di Rust!
Nell’iperspazio con Rocket: il Framework Web di Rust!Commit University
 
unit 4 immunoblotting technique complete.pptx
unit 4 immunoblotting technique complete.pptxunit 4 immunoblotting technique complete.pptx
unit 4 immunoblotting technique complete.pptxBkGupta21
 
Advanced Computer Architecture – An Introduction
Advanced Computer Architecture – An IntroductionAdvanced Computer Architecture – An Introduction
Advanced Computer Architecture – An IntroductionDilum Bandara
 
TeamStation AI System Report LATAM IT Salaries 2024
TeamStation AI System Report LATAM IT Salaries 2024TeamStation AI System Report LATAM IT Salaries 2024
TeamStation AI System Report LATAM IT Salaries 2024Lonnie McRorey
 
Generative AI for Technical Writer or Information Developers
Generative AI for Technical Writer or Information DevelopersGenerative AI for Technical Writer or Information Developers
Generative AI for Technical Writer or Information DevelopersRaghuram Pandurangan
 
How AI, OpenAI, and ChatGPT impact business and software.
How AI, OpenAI, and ChatGPT impact business and software.How AI, OpenAI, and ChatGPT impact business and software.
How AI, OpenAI, and ChatGPT impact business and software.Curtis Poe
 
Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024BookNet Canada
 
The Role of FIDO in a Cyber Secure Netherlands: FIDO Paris Seminar.pptx
The Role of FIDO in a Cyber Secure Netherlands: FIDO Paris Seminar.pptxThe Role of FIDO in a Cyber Secure Netherlands: FIDO Paris Seminar.pptx
The Role of FIDO in a Cyber Secure Netherlands: FIDO Paris Seminar.pptxLoriGlavin3
 
Commit 2024 - Secret Management made easy
Commit 2024 - Secret Management made easyCommit 2024 - Secret Management made easy
Commit 2024 - Secret Management made easyAlfredo García Lavilla
 
Dev Dives: Streamline document processing with UiPath Studio Web
Dev Dives: Streamline document processing with UiPath Studio WebDev Dives: Streamline document processing with UiPath Studio Web
Dev Dives: Streamline document processing with UiPath Studio WebUiPathCommunity
 
Ensuring Technical Readiness For Copilot in Microsoft 365
Ensuring Technical Readiness For Copilot in Microsoft 365Ensuring Technical Readiness For Copilot in Microsoft 365
Ensuring Technical Readiness For Copilot in Microsoft 3652toLead Limited
 
"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack
"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack
"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek SchlawackFwdays
 
Unraveling Multimodality with Large Language Models.pdf
Unraveling Multimodality with Large Language Models.pdfUnraveling Multimodality with Large Language Models.pdf
Unraveling Multimodality with Large Language Models.pdfAlex Barbosa Coqueiro
 
New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024BookNet Canada
 
Hyperautomation and AI/ML: A Strategy for Digital Transformation Success.pdf
Hyperautomation and AI/ML: A Strategy for Digital Transformation Success.pdfHyperautomation and AI/ML: A Strategy for Digital Transformation Success.pdf
Hyperautomation and AI/ML: A Strategy for Digital Transformation Success.pdfPrecisely
 

Kürzlich hochgeladen (20)

Are Multi-Cloud and Serverless Good or Bad?
Are Multi-Cloud and Serverless Good or Bad?Are Multi-Cloud and Serverless Good or Bad?
Are Multi-Cloud and Serverless Good or Bad?
 
DMCC Future of Trade Web3 - Special Edition
DMCC Future of Trade Web3 - Special EditionDMCC Future of Trade Web3 - Special Edition
DMCC Future of Trade Web3 - Special Edition
 
The Ultimate Guide to Choosing WordPress Pros and Cons
The Ultimate Guide to Choosing WordPress Pros and ConsThe Ultimate Guide to Choosing WordPress Pros and Cons
The Ultimate Guide to Choosing WordPress Pros and Cons
 
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)
 
Moving Beyond Passwords: FIDO Paris Seminar.pdf
Moving Beyond Passwords: FIDO Paris Seminar.pdfMoving Beyond Passwords: FIDO Paris Seminar.pdf
Moving Beyond Passwords: FIDO Paris Seminar.pdf
 
Nell’iperspazio con Rocket: il Framework Web di Rust!
Nell’iperspazio con Rocket: il Framework Web di Rust!Nell’iperspazio con Rocket: il Framework Web di Rust!
Nell’iperspazio con Rocket: il Framework Web di Rust!
 
unit 4 immunoblotting technique complete.pptx
unit 4 immunoblotting technique complete.pptxunit 4 immunoblotting technique complete.pptx
unit 4 immunoblotting technique complete.pptx
 
Advanced Computer Architecture – An Introduction
Advanced Computer Architecture – An IntroductionAdvanced Computer Architecture – An Introduction
Advanced Computer Architecture – An Introduction
 
TeamStation AI System Report LATAM IT Salaries 2024
TeamStation AI System Report LATAM IT Salaries 2024TeamStation AI System Report LATAM IT Salaries 2024
TeamStation AI System Report LATAM IT Salaries 2024
 
Generative AI for Technical Writer or Information Developers
Generative AI for Technical Writer or Information DevelopersGenerative AI for Technical Writer or Information Developers
Generative AI for Technical Writer or Information Developers
 
How AI, OpenAI, and ChatGPT impact business and software.
How AI, OpenAI, and ChatGPT impact business and software.How AI, OpenAI, and ChatGPT impact business and software.
How AI, OpenAI, and ChatGPT impact business and software.
 
Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
 
The Role of FIDO in a Cyber Secure Netherlands: FIDO Paris Seminar.pptx
The Role of FIDO in a Cyber Secure Netherlands: FIDO Paris Seminar.pptxThe Role of FIDO in a Cyber Secure Netherlands: FIDO Paris Seminar.pptx
The Role of FIDO in a Cyber Secure Netherlands: FIDO Paris Seminar.pptx
 
Commit 2024 - Secret Management made easy
Commit 2024 - Secret Management made easyCommit 2024 - Secret Management made easy
Commit 2024 - Secret Management made easy
 
Dev Dives: Streamline document processing with UiPath Studio Web
Dev Dives: Streamline document processing with UiPath Studio WebDev Dives: Streamline document processing with UiPath Studio Web
Dev Dives: Streamline document processing with UiPath Studio Web
 
Ensuring Technical Readiness For Copilot in Microsoft 365
Ensuring Technical Readiness For Copilot in Microsoft 365Ensuring Technical Readiness For Copilot in Microsoft 365
Ensuring Technical Readiness For Copilot in Microsoft 365
 
"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack
"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack
"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack
 
Unraveling Multimodality with Large Language Models.pdf
Unraveling Multimodality with Large Language Models.pdfUnraveling Multimodality with Large Language Models.pdf
Unraveling Multimodality with Large Language Models.pdf
 
New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
 
Hyperautomation and AI/ML: A Strategy for Digital Transformation Success.pdf
Hyperautomation and AI/ML: A Strategy for Digital Transformation Success.pdfHyperautomation and AI/ML: A Strategy for Digital Transformation Success.pdf
Hyperautomation and AI/ML: A Strategy for Digital Transformation Success.pdf
 

Representation state transfer and some other important stuff

  • 2. Warning before we start ‣ REST != MVC ‣ Do not think in controllers, id’s, actions, models, views, plugins, helpers etc... ‣ In fact, do not think about implementation AT ALL!!
  • 3. What is REST? Roy Fielding said: REST is a coordinated set of architectural constraints that attempts to minimize latency and network communication while at the same time maximizing the independence and scalability of component implementations. This is achieved by placing constraints on connector semantics where other styles have focused on component semantics. REST enables the caching and reuse of interactions, dynamic substitutability of components, and processing of actions by intermediaries, thereby meeting the needs of an Internet-scale distributed hypermedia system.
  • 4. What is REST? ‣ Cacheable ‣ Stateless ‣ Scalable ‣ Fault-tolerant ‣ Loosely coupled ‣ ADVANTAGES OF REST
  • 5. What is REST? ‣ URL identifies a resource ‣ URLs have an hierarchy ‣ Methods perform operations on resources ‣ Operation must be implicit ‣ Hypermedia format to represent data ‣ Link relations to navigate ‣ THE PRINCIPLES OF REST
  • 6. What is REST? ‣ Identification of resources ‣ Manipulation of resources ‣ Self-descriptive messages ‣ HATEOAS ‣ THE FOUR MAIN PRINCIPLES
  • 7. What is REST? ‣ Identification of resources ‣ Manipulation of resources ‣ Self-descriptive messages ‣ HATEOAS ‣ THE FOUR MAIN PRINCIPLES
  • 8. Identification of resources ‣ /index.php?action=getarticle&id=5 ‣ /default/article/5/4/6/size Cacheable? Scalable? Readable? ‣ You are doing it wrong... :(
  • 9. Identification of resources ‣ /articles We want all articles ‣ /articles/5/photos/4/comments/1 We want the first comment of the fourth photo for the fifth article ‣ /articles/5/photos/4/comments We want all comments of the fourth photo for the fifth article Cacheable! Scalable! Readable! ‣ Readable and maintainable!
  • 10. Identification of resources ✗ /photos/order/size/limit/5 ✗ /photos/limit/5/order/size ✓ /photos?order=size&limit=5 ✓ /photos?limit=5&order=size ‣ FILTERING THROUGH A QUERY STRING, NOT THE URI
  • 11. What is REST? ‣ Identification of resources ‣ Manipulation of resources ‣ Self-descriptive messages ‣ HATEOAS ‣ THE FOUR MAIN PRINCIPLES
  • 12. Manipulation of resources ‣ Create ‣ Retrieve ‣ Update ‣ Delete ‣ But please note that REST != CRUD
  • 13. Manipulation of resources ‣ Create = PUT ‣ Retrieve = GET ‣ Update = POST ‣ Delete = DELETE ‣ CRUD to HTTP verb mapping
  • 14. Manipulation of resources ‣ Any client should be able to make the request as many times as necessary. ‣ GET, OPTIONS, HEAD ‣ SAFE METHODS
  • 15. Manipulation of resources ‣ Garantuees that the client can repeat the request when it’s not certain. ‣ $x++ vs $x=4 ‣ ALL METHODS EXCEPT “POST” ‣ IDEMPOTENT METHODS
  • 16. What is REST? ‣ Identification of resources ‣ Manipulation of resources ‣ Self-descriptive messages ‣ HATEOAS ‣ THE FOUR MAIN PRINCIPLES
  • 17. Self-descriptive messages ‣ Stateless! ‣ All information for processing is available: ‣ How? (method + content-type) ‣ What? (URI) ‣ When? (preconditions) ‣ Who? (authentication)
  • 18. Self-descriptive messages GET /article/1234 HTTP/1.1 Host: www.enrise.com Accept: application/vnd.enrise.nl+xml ; version: 1.0 Authorization: OAuth oauth_nonce=”123” ... If-None-Matched: absad12412414 ‣ HOW (method)
  • 19. Self-descriptive messages GET /article/1234 HTTP/1.1 Host: www.enrise.com Accept: application/vnd.enrise.nl+xml ; version: 1.0 Authorization: OAuth oauth_nonce=”123” ... If-None-Matched: absad12412414 ‣ HOW (content-type)
  • 20. Self-descriptive messages GET /article/1234 HTTP/1.1 Host: www.enrise.com Accept: application/vnd.enrise.nl+xml ; version: 1.0 Authorization: OAuth oauth_nonce=”123” ... If-None-Matched: absad12412414 ‣ WHAT
  • 21. Self-descriptive messages GET /article/1234 HTTP/1.1 Host: www.enrise.com Accept: application/vnd.enrise.nl+xml ; version: 1.0 Authorization: OAuth oauth_nonce=”123” ... If-None-Matched: absad12412414 ‣ WHEN
  • 22. Self-descriptive messages GET /article/1234 HTTP/1.1 Host: www.enrise.com Accept: application/vnd.enrise.nl+xml ; version: 1.0 Authorization: OAuth oauth_nonce=”123” ... If-None-Matched: absad12412414 ‣ WHO
  • 23. What is REST? ‣ Identification of resources ‣ Manipulation of resources ‣ Self-descriptive messages ‣ HATEOAS ‣ THE FOUR MAIN PRINCIPLES
  • 24. HATEOAS HATEOAS = Hypermedia As The Engine Of Application State
  • 25. HATEOAS This is the hardest and of course, most important part of REST
  • 26. HATEOAS ‣ Use links to allow clients to discover locations and operations. ‣ Link relations are used to express options. ‣ Clients do not need to know URLs. ‣ This controls the state.
  • 27. State inside your REST API Fetch Select Confirm Pay E-Ticket ‣ “Flight booking API”
  • 28. State inside your REST API POST /search?order=price&limit=5 HTTP/1.1 Host: www.enrise.com Accept: application/vnd.enrise.nl+xml ; version: 1.0 <search> <destination>LPA</destination> <date>24-may-2011</date> <type>firstclass</type> </search> ‣ Search for specified flights
  • 29. State inside your REST API HTTP/1.1 200 OK Content-type: application/vnd.enrise.nl+xml ; version: 1.0 <flights xmlns=...> <flight> <flightno>KL1234</flightno> <time>4:24</time> <link method=”get” rel=”details” action=”/flight/15263” type=”text/xml”> <link method=”post” rel=”confirm” action=”/confirm/flight/15263” type=”text/xml”> </flight> <flight> <flightno>HV123</flightno> <time>3:54</time> <link method=”get” rel=”details” action=”/flight/523525” type=”text/xml”> <link method=”post” rel=”confirm” action=”/confirm/flight/523525” type=”text/xml”> </flight> </flights> ‣ Returns a collection of flights
  • 30. State inside your REST API POST /confirm/flight/15263 HTTP/1.1 Host: www.enrise.com Accept: application/vnd.enrise.nl+xml ; version: 1.0 <flight> <seat>2A</seat> <meal>vegetarian</meal> </flight> HTTP/1.1 401 Authentication required ‣ Confirm a specific flight
  • 31. State inside your REST API POST /confirm/flight/15263 HTTP/1.1 Host: www.enrise.com Accept: application/vnd.enrise.nl+xml ; version: 1.0 Authorization: OAuth ...... <flight> <seat>2A</seat> <meal>vegetarian</meal> </flight> HTTP/1.1 200 OK Location: /booking/1616163 <booking> <link rel=”details” href=”/booking/1616163” method=”get” type=”application/xml”> <link rel=”payment” href=”/payment/booking/1616163” type=”application/xml”> <link rel=”cancel” href=”/booking/1616163” method=”delete” type=”application/xml”> </booking> ‣ Confirm a specific flight, with more info
  • 32. State inside your REST API OPTIONS /booking/1616163 HTTP/1.1 Host: www.enrise.com Authorization: OAuth ...... HTTP/1.1 200 OK Allow: GET, DELETE, PUT ‣ What can we do with our booking?
  • 33. State inside your REST API DELETE /booking/1616163 HTTP/1.1 Host: www.enrise.com Authorization: OAuth ...... HTTP/1.1 204 No content ‣ Cancel our booking!
  • 34. State inside your REST API GET /booking/1616163 HTTP/1.1 Host: www.enrise.com Accept: application/vnd.enrise.nl+xml ; version: 1.0 Authorization: OAuth... HTTP/1.1 200 OK <booking> <flight> <flightno>KL1234</flightno> <time>4:24</time> <link rel=”details” method=”get” href=”/flight/15263”> </flight> <payment> <status>Not paid</status> <link rel=”details” method=”get” href=”/payment/booking/1616163”> <link rel=”pay” method=”post” href=”/payment/booking/1616163”> </payment> </booking> ‣ Still need to pay for the flight
  • 35. State inside your REST API PUT /payment/booking/1616163 HTTP/1.1 Host: www.enrise.com Accept: application/vnd.enrise.nl+xml ; version: 1.0 Authorization: OAuth ...... <payment> <cardno>4111-1111-1111-1111</cardno> <expires>04/13</expires> <name>Joshua Thijssen</name> <amount currency=”eur”>414.00</amount> </payment> HTTP/1.1 201 Created Location: /payment/booking/1616163 ‣ Pay through another resource
  • 36. State inside your REST API OPTIONS /booking/1616163 HTTP/1.1 Host: www.enrise.com Authorization: OAuth ...... HTTP/1.1 200 OK Allow: GET ‣ Can’t delete our booking since it’s paid
  • 37. State inside your REST API GET /booking/1616163 HTTP/1.1 Host: www.enrise.com Accept: application/vnd.enrise.nl+xml ; version: 1.0 Authorization: OAuth... HTTP/1.1 200 OK <booking> <flight> <flightno>KL1234</flightno> <time>4:24</time> <link rel=”details” method=”get” href=”/flight/15263”> </flight> <payment> <status>Paid in full</status> <link rel=”details” method=”get” href=”/payment/booking/1616163”> </payment> <link rel=”eticket” method=”get” href=”/eticket/12415156261616”> </booking> ‣ We can fetch our eticket now
  • 38. (Common) pitfalls of REST design ‣ Versioning ‣ Methods in uri ‣ One uri per resource ‣ Controller resources & non-CRUD
  • 39. (Common) pitfalls of REST design ‣ Versioning ‣ Methods in uri ‣ One uri per resource ‣ Controller resources & Non-CRUD
  • 40. (Common) pitfalls of REST design ‣ /api/v1.1/article/1234/photos ‣ /api/v1.2/article/1234/photos ‣ Different resources? ‣ Versioning
  • 41. (Common) pitfalls of REST design GET /api/article/1234/photos HTTP/1.1 Host: www.enrise.com Accept: application/vnd.enrise.nl+xml ; version = 1.0 GET /api/article/1234/photos HTTP/1.1 Host: www.enrise.com Accept: application/vnd.enrise.nl+json ; version = 1.1 GET /api/article/1234/photos HTTP/1.1 Host: www.enrise.com Accept: application/vnd.enrise.nl+json ; version = 5.0.4a ‣ Versioning
  • 42. (Common) pitfalls of REST design ‣ Versioning ‣ Methods in uri ‣ One uri per resource ‣ Controller resources & Non-CRUD
  • 43. (Common) pitfalls of REST design ‣ /api/get/articles/1234/photos ‣ /api/articles/new ‣ /api/articles/list ‣ Methods in URL
  • 44. (Common) pitfalls of REST design ‣ Versioning ‣ Methods in uri ‣ One uri per resource ‣ Controller resources & Non-CRUD
  • 45. (Common) pitfalls of REST design ‣ /api/article/1234 ‣ /api/article/red+teddybear ‣ Different resources ‣ One URI per resource
  • 46. (Common) pitfalls of REST design GET /api/article/red+teddybear HTTP/1.1 Host: www.enrise.com Accept: application/vnd.enrise.nl+xml ; version = 1.0 HTTP/1.1 303 See Other Location: /api/article/1234 ‣ One URI per resource
  • 47. (Common) pitfalls of REST design ‣ Versioning ‣ Methods in uri ‣ One uri per resource ‣ Controller resources & Non-CRUD
  • 48. (Common) pitfalls of REST design ‣ Outside the CRUD? ‣ Multiple operations simultaneously? ‣ Controller resources & non-crud
  • 49. (Common) pitfalls of REST design POST /distance HTTP/1.1 GET /distance?from=...&to=.. HTTP/1.1 Host: www.enrise.com Host: www.enrise.com Accept: text/xml Accept: text/xml Content-type: text/xml;charset=UTF-8 <xml> <from>Amersfoort, NLD</from> <to>Apeldoorn, NLD</to> </xml> HTTP/1.1 200 OK Content-length: 123 Content-type: text/xml <result> <distance unit=”km”>45</distance> <distance unit=”miles”>27.96</distance> </result> ‣ Controller resources & non-crud
  • 50. (Common) pitfalls of REST design POST /user/jthijssen/address_merge HTTP/1.1 Host: www.enrise.com Accept: application/vnd.enrise.nl+xml ; version = 1.0 Content-type: text/csv;charset=UTF-8 John Doe, 1 Main Street, Seattle, WA Jane Doe, 100 North Street, Los Angeles, CA HTTP/1.1 303 See Other Location: /user/jthijssen/addressbook ‣ Controller resources & non-crud
  • 51. More important stuff ‣ HTTP Status codes ‣ ETags
  • 52. HTTP Status codes ‣ Status codes are important ‣ They represent the result of your actions
  • 53. HTTP Status codes ‣ 1xx Informational ‣ 2xx Success ‣ 3xx Redirection ‣ 4xx Client error ‣ 5xx Server error
  • 54. HTTP Status codes ‣ 200 OK Resource returned ‣ 201 Created Resource created ‣ 204 No content Resource deleted ‣ IMPORTANT 2xx CODES
  • 55. HTTP Status codes ‣ 304 Not modified Resource wasn’t changed ‣ IMPORTANT 3xx CODES
  • 56. HTTP Status codes ‣ 400 Bad request Incorrect payload ‣ 401 Unauthorized Not authorized to operate ‣ 403 Forbidden Not authorized to operate ‣ 404 Not found Resource was not found ‣ IMPORTANT 4xx CODES
  • 57. HTTP Status codes ‣ 405 Method not allowed Method incorrect ‣ 406 Not acceptable Cannot return in correct format ‣ 412 Precondition failed “ETag mismatch” ‣ IMPORTANT 4xx CODES
  • 58. HTTP Status codes ‣ 500 Internal server error “Something” happened ‣ 501 Not implemented Method is not implemented ‣ IMPORTANT 5xx CODES
  • 59. HTTP Status codes ‣ 501 Not implemented vs 405 Method not allowed ‣ 409 Conflict vs 412 Precondition failed ‣ de·bat·a·ble/diˈbātəbəl/Adjective
  • 60. ETags & Optimistic locking GET /blogpost/12345 HTTP/1.1 Host: www.enrise.com HTTP/1.1 200 OK Content-length: 1234000 Content-type: text/xml ETag: abcd-1234 <xml> <blogpost> <author>Joshua Thijssen</author> <title>...</title> .... </blogpost> </xml>
  • 61. ETags & Optimistic locking GET /blogpost/12345 HTTP/1.1 Host: www.enrise.com If-None-Match: abcd-1234 HTTP/1.1 304 Not modified Blogpost is cached and can be used!
  • 62. ETags & Optimistic locking POST /blogpost/12345 HTTP/1.1 Host: www.enrise.com If-Match: abcd-1234 <xml> <author>Sjors de Valk</author> </xml> HTTP/1.1 412 Precondition failed Blogpost is already modified by “someone”
  • 63. REST examples Other REST Examples
  • 64. REST examples PUT /articles HTTP/1.1 Host: www.enrise.com Content-type: application/vnd.enrise.nl+xml ; version = 1.0 <article xmlns=”http://www.enrise.com/article”> <name>Teddybear</name> <color>red</color> <stock>15</stock> <price currency=”eur”>15,95</price> <price currency=”usd”>19,95</price> </article> HTTP/1.1 201 Created Location: /articles/1234 ‣ Creating a resource
  • 65. REST examples GET /articles HTTP/1.1 Host: www.enrise.com Accept: application/vnd.enrise.nl+xml ; version = 1.0 HTTP/1.1 200 OK Content-length: 12345 Content-type: application/vnd.enrise.nl+xml Date: sun, 01 Nov 2010 12:34:56 GMT <articles xmlns=”http://www.enrise.com/article”> <article> <name>Teddybear</name> <link method=”get” rel=”article” href=”/articles/1234”> </article> <article> <name>Skippyball</name> <link method=”get” rel=”article” href=”/articles/1121”> </article> </articles> ‣ Getting a resource collection
  • 66. REST examples GET /articles/1234 HTTP/1.1 Host: www.enrise.com Accept: application/vnd.enrise.nl+xml ; version = 1.0 HTTP/1.1 200 OK Content-length: 12345 Content-type: application/vnd.enrise.nl+xml Date: sun, 01 Nov 2010 12:34:56 GMT <article xmlns=”http://www.enrise.com/article”> <name>Teddybear</name> ... </article> HTTP/1.1 404 Not found Content-length: 0 Date: sun, 01 Nov 2010 12:34:56 GMT ‣ Getting a resource
  • 67. REST examples DELETE /articles/1234 HTTP/1.1 Host: www.enrise.com Accept: application/vnd.enrise.nl+xml ; version = 1.0 HTTP/1.1 204 No content Content-length: 0 Date: sun, 01 Nov 2010 12:34:56 GMT ‣ Delete a resource
  • 68. REST examples POST /articles/1234 HTTP/1.1 Host: www.enrise.com Accept: application/vnd.enrise.nl+xml ; version = 1.0 Idempotent If-Match: 23709-12135125 <article xmlns=”http://www.enrise.com/article> <name>Teddybear</name> <color>red</color> <stock>30</stock> <price currency=”eur”>15,95</price> <price currency=”usd”>19,95</price> </article> HTTP/1.1 200 OK Content-length: 0 Date: sun, 01 Nov 2010 12:34:56 GMT HTTP/1.1 412 Precondition failed Content-length: 0 Date: sun, 01 Nov 2010 12:34:56 GMT ‣ Updating a resource
  • 69. (Common) pitfalls of REST design POST /user/jthijssen/address_merge HTTP/1.1 Host: www.enrise.com Accept: application/vnd.enrise.nl+xml ; version = 1.0 Content-type: text/csv;charset=UTF-8 John Doe, 1 Main Street, Seattle, WA Jane Doe, 100 North Street, Los Angeles, CA HTTP/1.1 303 See Other Location: /user/jthijssen/addressbook ‣ Controller resources
  • 70. More reading ‣ http://en.wikipedia.org/wiki/Representational_State_Transfer ‣ http://www.ics.uci.edu/~fielding/pubs/dissertation/top.htm ‣ http://oreilly.com/catalog/9780596801694 ‣ http://www.slideshare.net/Wombert/designing-http-interfaces-and- restful-web-services-confoo11-20110310 ‣ http://www.slideshare.net/adorepump/hateoas-the-confusing-bit- from-rest ‣ http://www.slideshare.net/guilhermecaelum/rest-in-practice
  • 72. ‣ THANK YOU FOR YOUR ATTENTION

Hinweis der Redaktion

  1. \n
  2. \n
  3. \n
  4. \n
  5. \n
  6. \n
  7. \n
  8. \n
  9. \n
  10. \n
  11. \n
  12. \n
  13. \n
  14. \n
  15. \n
  16. \n
  17. \n
  18. \n
  19. \n
  20. \n
  21. \n
  22. \n
  23. \n
  24. \n
  25. \n
  26. \n
  27. \n
  28. \n
  29. \n
  30. \n
  31. \n
  32. \n
  33. \n
  34. \n
  35. \n
  36. \n
  37. \n
  38. \n
  39. \n
  40. \n
  41. \n
  42. \n
  43. \n
  44. \n
  45. \n
  46. \n
  47. \n
  48. \n
  49. \n
  50. \n
  51. \n
  52. \n
  53. \n
  54. \n
  55. \n
  56. \n
  57. \n
  58. \n
  59. \n
  60. \n
  61. \n
  62. \n
  63. \n
  64. \n
  65. \n
  66. \n
  67. \n
  68. \n
  69. \n
  70. \n
  71. \n
  72. \n