SlideShare ist ein Scribd-Unternehmen logo
1 von 39
Downloaden Sie, um offline zu lesen
Building REST and
               Hypermedia APIs with PHP



                         International PHP Conference 2012 Tbilisi, Georgia
Wednesday, December 12, 12
About me


    ‣ Ioseb Dzmanashvili
    ‣ Software Architect at AzRy LLC
    ‣ Teacher at Caucasus School of Technology
    ‣ V8 JavaScript engine contributor
    ‣ Author of uri_template PHP extension
    ‣ Author of Create-Form and Edit-Form link relation
           types (being RFCed now).

Wednesday, December 12, 12
REST
                             Representational State Transfer




Wednesday, December 12, 12
What is REST?




    ‣ It’s not a specification
    ‣ It’s not a design pattern
    ‣ It’s not... bla bla bla...
    ‣ It’s an architectural style

Wednesday, December 12, 12
Architectural Style
                                    Definition




         An architectural style is a coordinated set of
      architectural constraints that restricts the roles/features
      of architectural elements and the allowed relationships
        among those elements within any architecture that
                       conforms to that style.

                                  Roy T. Fielding 2000 (quote from dissertation)




Wednesday, December 12, 12
Architectural Style
                                     Definition


      An architectural style is a named
         collection of architectural design
      decisions that: 1) are applicable in a
            given development context; 2)
      constrain architectural decision that
        are specific to a particular system
        within that context; and 3) elicit
           beneficial qualities in each
                       resulting system.

                                Richard N. Taylor et al.

Wednesday, December 12, 12
Analogy
                             Gothic Architecture




Wednesday, December 12, 12
Analogy
                             Georgian Church Architecture




Wednesday, December 12, 12
Analogy
                             Industrial Architecture




Wednesday, December 12, 12
REST Constraints
   • Client Server: Separation of concerns is the principle behind the client-server
       constraints. ...Perhaps most significant to the Web, however, is that the separation
       allows the components to evolve independently.

   • Stateless: ...communication must be stateless in nature, ...such that each request
       from client to server must contain all of the information necessary to
       understand the request, and cannot take advantage of any stored context on the
       server

   • Cache: The advantage of adding cache constraints is that they have the potential to
       partially or completely eliminate some interactions, improving efficiency, scalability,
       and user-perceived performance by reducing the average latency of a series of
       interactions.

   • Layered System: The layered system style allows an architecture to be composed of
       hierarchical layers by constraining component behavior such that each component
       cannot "see" beyond the immediate layer with which they are interacting.

   • Code-On-Demand: (Optional constraint)
   • Uniform Interface: (To be continued)
Wednesday, December 12, 12
Uniform Interface
       By applying the software engineering principle of generality to
       the component interface, the overall system architecture is
       simplified and the visibility of interactions is improved.
       Implementations are decoupled from the services they provide,
       which encourages independent evolvability.

       In order to obtain a uniform interface, multiple architectural
       constraints are needed to guide the behavior of components.
       REST is defined by four interface constraints:

           • identification of resources;
           • manipulation of resources through
               representations;
           •   self-descriptive messages; and,
           •   hypermedia as the engine of application state.
Wednesday, December 12, 12
Hypermedia




Wednesday, December 12, 12
HTTP



         The Hypertext Transfer Protocol (HTTP) is an
       application-level protocol for distributed, collaborative,
                  hypertext information systems.

                              Hypertext Transfer Protocol RFC, HTTPbis




Wednesday, December 12, 12
Hypertext



      By "hypertext", I mean non-sequential writing - text
     that branches and allows choices to the reader, best read
      at an interactive screen. As popularity conceived, this
       is a series of text chunks connected by links
        which offer the reader different pathways.

                                            Theodor Nelson 1960s




Wednesday, December 12, 12
Hypertext


          An intriguing possibility, given a
           large hypertext database with
        typed links, is that it allows some
         degree of automatic analysis.
                             Tim Berners-Lee 1989, CERN
                                  (Initial WWW proposal)




Wednesday, December 12, 12
Hypermedia



            Hypermedia simply extends the
             notion of the text in hypertext by
          including visual information, sound,
          animation, and other forms of data.

                                 George P. Landow




Wednesday, December 12, 12
Hypermedia

                     Hypermedia system, contains
                      various types of relationships
                     between elements of information.

                Hypermedia allows these
      relationships to be instantiated as
         links which connect the various
       information elements, so that these
      links can be used to navigate within the
                           information space.

                              David Lowe, Wendy Hau 1999

Wednesday, December 12, 12
Hypermedia


        Hypermedia: An application which
            uses associative relationships
        among information contained within
       multiple media data for the purpose of
                 facilitating access to, and
              manipulation of, information
                     encapsulated by the data

                             David Lowe, Wendy Hau 1999


Wednesday, December 12, 12
Hypertext

        When I say hypertext, I mean the
        simultaneous presentation of
        information and controls such
          that the information becomes the
     affordance through which the user (or
            automaton) obtains choices and
                             selects actions

                              Roy T. Fielding 2008




Wednesday, December 12, 12
Examples of controls?

       <!-- non visible link -->
       <link rel="stylesheet" type="text/css" href="styles.css">

       <!-- outgoing link -->
       <a href="/post/1/author" rel="author">John Doe</a>

       <!-- embed link(transclusion) -->
       <img src="/logo.jpg" alt="IBM Logo">

       <!-- templated link. produces: /search?q=term -->
       <form method="get" action="/search">
          <label>Search: </label><input name="q">
       </form>

       <!-- action link -->
       <form method="post" action="/posts" type="text/plain">
          <textarea>Representational State Transfer</textarea>
       </form>

Wednesday, December 12, 12
Photo Service Example




Wednesday, December 12, 12
The Story




                Hey folks, we’ve created amazing photo sharing
                  service and we have an API! check it out:
                           http://service.org/photos




Wednesday, December 12, 12
Let’s try it
                                  List of Photos


                             GET /photos HTTP/1.1
                             Host: service.org


                              1


                                    Request



                                                  Response
                                              2
      HTTP/1.1 200 OK
      Link: </photos/1>; rel="enclosure"; type="image/jpg"; title= "Egypt",
         </photos/2>; rel="enclosure"; type="image/jpg"; title="Paris",
         </photos/3>; rel="enclosure"; type="image/jpg"; title="Tbilisi",
         </photos/upload-form>; rel="create-form"; title="Upload photo"




Wednesday, December 12, 12
What’s this Link Header?
                             Quotes from Web Linking spec(RFC5988)

      • Link is a typed connection between two resources
             that are identified by Internationalised Resource   Identifiers
             (IRIs)

      • Links between resources need not be format
             specific; it can be useful to have typed links that are
             independent of their serialization.

      • The relation type of a link is conveyed in the "rel"
             parameter's value.

      • The "type" parameter, when present, is a hint
             indicating what the media type of the result of
             dereferencing the link should be.
Wednesday, December 12, 12
Meaning of Relations
                                 Quotes from specs




      • The value "enclosure" signifies a related resource
             that is potentially large and might require special
             handling.

      • The value "create-form" signifies a related
             resource where a submission form can be obtained.



Wednesday, December 12, 12
Fetching Photo (Client A)
                                                       Request
                         GET /photos/1 HTTP/1.1
           1             Host: service.org


                                        HTTP/1.1 200 OK
                                        Content-Type: image/jpeg
                             Response   Content-Length: 1000
                                        Cache-Control: private, max-age=0   2
                                        ETag: a32lasdf

                                        [PHOTO HERE]



                         GET /photos/1 HTTP/1.1          Request
           3             Host: service.org
                         If-None-Match: a32lasdf


                             Response
                                        HTTP/1.1 304 Not Modified           4
Wednesday, December 12, 12
Client B Deletes Photo
                                                            Request
                       DELETE /photos/1 HTTP/1.1
         1             Host: service.org

                                        Response
                                                       HTTP/1.1 204 No Content    2


                              Client A Fetches it Again
                             GET /photos/1 HTTP/1.1         Request
           3                 Host: service.org
                             If-None-Match: a32lasdf



                                  HTTP/1.1 404 Not Found
           Response               Content-Type: ...
                                  Content-Length: …                               4
                                  Link: </photos>; rel="index"; title="Photos",
                                   </photos/upload-form>; rel="create-form";

Wednesday, December 12, 12
Uploading Photo (Step 1)

                       GET /photos/upload-form HTTP/1.1
                                                            Request
            1          Host: service.org




                              HTTP/1.1 200 OK
                              Content-Type: application/vnd.forms+json
                              Content-Length: …

            Response          {
                                  "method": "POST",                          2
                                  "action": "/photos",
                                  "content-type": [
                                   "image/jpeg", "image/png", "image/gif",
                                  ]
                              }



Wednesday, December 12, 12
Uploading Photo (Step 2)
                         POST /photos HTTP/1.1
                         Host: service.org              Request
                         Content-Type: image/jpeg
           1             Content-Length: 3000

                         [PHOTO HERE]



               Response          HTTP/1.1 202 Accepted
                                 Link: </photos/queue/e3ba8>; rel="monitor"   2
                                 Retry-After: 10



                                                            Request
                         GET /photos/queue/e3ba8 HTTP/1.1
           3             Host: service.org


                                        Response     HTTP/1.1 303 See Other
                                                     Location: /photos/1234
                                                                              4
Wednesday, December 12, 12
Quick advices




Wednesday, December 12, 12
Do not use xml or json directly

   Do not use these:

                ‣      application/xml

                ‣      application/json



   Instead try:

               ‣      application/atom+xml

               ‣      application/vnd.collection+json

               ‣      application/vnd.hal+json

               ‣      or design your own.
Wednesday, December 12, 12
Never encode action in URI
   Wrong:

                ‣      /some/resource/delete

                ‣      /some/resource/update



   Correct:

                ‣      DELETE /some/resource HTTP/1.1

                ‣      POST /some/resource




Wednesday, December 12, 12
Do not Abuse Protocol

     Wrong:
                HTTP/1.1 200 OK
                Content-Type: application/json
                Content-Length: …

                {"error": {
                   "code": 1234,
                   "message": "Page not found"
                }}




     Correct:
               HTTP/1.1 404 Not Found
               Content-Type: application/json
               Content-Length: …



Wednesday, December 12, 12
Do not Use URIs Directly

                             HTTP/1.1 404 Not Found
                             Content-Type: ...
                             Content-Length: …
                             Link: </photos>; rel="index"; title="Photos",
                              </photos/upload-form>; rel="create-form";




                             HTTP/1.1 404 Not Found
                             Content-Type: ...
                             Content-Length: …
                             Link: </photos>; rel="index"; title="Photos",
                              </photos/upload-form>; rel="create-form";




Wednesday, December 12, 12
Do not hardcode control
                       information in your code
       HTTP/1.1 200 OK
       Content-Type: application/vnd.forms+json
       Content-Length: …

       {
            "method": "POST",
            "action": "/photos",
            "content-type": [
             "image/jpeg",         HTTP/1.1 200 OK
            ]                      Content-Type: application/vnd.forms+json
       }                           Content-Length: …

                                   {
                                       "method": "POST",
                                       "action": "/photos",
                                       "content-type": [
                                        "image/jpeg",
                                       ]
                                   }

Wednesday, December 12, 12
Links in JSON




Wednesday, December 12, 12
And where is PHP?




                               sorry... i lied :(




Wednesday, December 12, 12
Questions?




Wednesday, December 12, 12
Thank You!




Wednesday, December 12, 12

Weitere ähnliche Inhalte

Was ist angesagt?

WebLogic Developer Webcast 1: JPA 2.0
WebLogic Developer Webcast 1: JPA 2.0WebLogic Developer Webcast 1: JPA 2.0
WebLogic Developer Webcast 1: JPA 2.0
Jeffrey West
 

Was ist angesagt? (20)

JAX-RS 2.0: RESTful Web Services
JAX-RS 2.0: RESTful Web ServicesJAX-RS 2.0: RESTful Web Services
JAX-RS 2.0: RESTful Web Services
 
Introduction to the Web API
Introduction to the Web APIIntroduction to the Web API
Introduction to the Web API
 
Survey of restful web services frameworks
Survey of restful web services frameworksSurvey of restful web services frameworks
Survey of restful web services frameworks
 
CSS Architecture: Writing Maintainable CSS
CSS Architecture: Writing Maintainable CSSCSS Architecture: Writing Maintainable CSS
CSS Architecture: Writing Maintainable CSS
 
Building Killer RESTful APIs with NodeJs
Building Killer RESTful APIs with NodeJsBuilding Killer RESTful APIs with NodeJs
Building Killer RESTful APIs with NodeJs
 
RESTing with JAX-RS
RESTing with JAX-RSRESTing with JAX-RS
RESTing with JAX-RS
 
ReST (Representational State Transfer) Explained
ReST (Representational State Transfer) ExplainedReST (Representational State Transfer) Explained
ReST (Representational State Transfer) Explained
 
Spring HATEOAS
Spring HATEOASSpring HATEOAS
Spring HATEOAS
 
LAJUG Napster REST API
LAJUG Napster REST APILAJUG Napster REST API
LAJUG Napster REST API
 
Modern Web Applications
Modern Web ApplicationsModern Web Applications
Modern Web Applications
 
Andrei shakirin rest_cxf
Andrei shakirin rest_cxfAndrei shakirin rest_cxf
Andrei shakirin rest_cxf
 
Enterprise Spring Building Scalable Applications
Enterprise Spring Building Scalable ApplicationsEnterprise Spring Building Scalable Applications
Enterprise Spring Building Scalable Applications
 
WebLogic Developer Webcast 1: JPA 2.0
WebLogic Developer Webcast 1: JPA 2.0WebLogic Developer Webcast 1: JPA 2.0
WebLogic Developer Webcast 1: JPA 2.0
 
The Glory of Rest
The Glory of RestThe Glory of Rest
The Glory of Rest
 
RESTful Web services using JAX-RS
RESTful Web services using JAX-RSRESTful Web services using JAX-RS
RESTful Web services using JAX-RS
 
Spring Social - Messaging Friends & Influencing People
Spring Social - Messaging Friends & Influencing PeopleSpring Social - Messaging Friends & Influencing People
Spring Social - Messaging Friends & Influencing People
 
Docker meetup-nyc-v1
Docker meetup-nyc-v1Docker meetup-nyc-v1
Docker meetup-nyc-v1
 
REST in Practice
REST in PracticeREST in Practice
REST in Practice
 
Overview of RESTful web services
Overview of RESTful web servicesOverview of RESTful web services
Overview of RESTful web services
 
Building RESTful applications using Spring MVC
Building RESTful applications using Spring MVCBuilding RESTful applications using Spring MVC
Building RESTful applications using Spring MVC
 

Ähnlich wie Building REST and Hypermedia APIs with PHP

Cs 1023 lec 13 web (week 4)
Cs 1023 lec 13 web (week 4)Cs 1023 lec 13 web (week 4)
Cs 1023 lec 13 web (week 4)
stanbridge
 
SemTechBiz 2012 Panel on Linking Enterprise Data
SemTechBiz 2012 Panel on Linking Enterprise DataSemTechBiz 2012 Panel on Linking Enterprise Data
SemTechBiz 2012 Panel on Linking Enterprise Data
3 Round Stones
 
Introduction to REST and Jersey
Introduction to REST and JerseyIntroduction to REST and Jersey
Introduction to REST and Jersey
Chris Winters
 
Linked Data Planet Key Note
Linked Data Planet Key NoteLinked Data Planet Key Note
Linked Data Planet Key Note
rumito
 
Approaches To Research And Critical Writing
Approaches To Research And Critical WritingApproaches To Research And Critical Writing
Approaches To Research And Critical Writing
Hend Al-Khalifa
 
DIGITAL LIBRARIES
DIGITAL LIBRARIESDIGITAL LIBRARIES
DIGITAL LIBRARIES
viedma2
 
Developing Distributed Web Applications, Where does REST fit in?
Developing Distributed Web Applications, Where does REST fit in?Developing Distributed Web Applications, Where does REST fit in?
Developing Distributed Web Applications, Where does REST fit in?
Srinath Perera
 
Introduction to the Semantic Web
Introduction to the Semantic WebIntroduction to the Semantic Web
Introduction to the Semantic Web
Tomek Pluskiewicz
 
17 applied architectures
17 applied architectures17 applied architectures
17 applied architectures
Majong DevJfu
 

Ähnlich wie Building REST and Hypermedia APIs with PHP (20)

090626cc tech-summit
090626cc tech-summit090626cc tech-summit
090626cc tech-summit
 
Conclusions - Linked Data
Conclusions - Linked DataConclusions - Linked Data
Conclusions - Linked Data
 
Cs 1023 lec 13 web (week 4)
Cs 1023 lec 13 web (week 4)Cs 1023 lec 13 web (week 4)
Cs 1023 lec 13 web (week 4)
 
SemTechBiz 2012 Panel on Linking Enterprise Data
SemTechBiz 2012 Panel on Linking Enterprise DataSemTechBiz 2012 Panel on Linking Enterprise Data
SemTechBiz 2012 Panel on Linking Enterprise Data
 
Hunt for Domain Controller : Active Directory Pentesting Session
Hunt for Domain Controller : ActiveDirectory Pentesting SessionHunt for Domain Controller : ActiveDirectory Pentesting Session
Hunt for Domain Controller : Active Directory Pentesting Session
 
Introduction to REST and Jersey
Introduction to REST and JerseyIntroduction to REST and Jersey
Introduction to REST and Jersey
 
Intelligent expert systems for location planning
Intelligent expert systems for location planningIntelligent expert systems for location planning
Intelligent expert systems for location planning
 
Linked Data to Improve the OER Experience
Linked Data to Improve the OER ExperienceLinked Data to Improve the OER Experience
Linked Data to Improve the OER Experience
 
dsnotify presentation at www2010
dsnotify presentation at www2010 dsnotify presentation at www2010
dsnotify presentation at www2010
 
RDBMS to NoSQL. An overview.
RDBMS to NoSQL. An overview.RDBMS to NoSQL. An overview.
RDBMS to NoSQL. An overview.
 
Linked Data Planet Key Note
Linked Data Planet Key NoteLinked Data Planet Key Note
Linked Data Planet Key Note
 
Approaches To Research And Critical Writing
Approaches To Research And Critical WritingApproaches To Research And Critical Writing
Approaches To Research And Critical Writing
 
Journalism and the Semantic Web
Journalism and the Semantic WebJournalism and the Semantic Web
Journalism and the Semantic Web
 
DIGITAL LIBRARIES
DIGITAL LIBRARIESDIGITAL LIBRARIES
DIGITAL LIBRARIES
 
Developing Distributed Web Applications, Where does REST fit in?
Developing Distributed Web Applications, Where does REST fit in?Developing Distributed Web Applications, Where does REST fit in?
Developing Distributed Web Applications, Where does REST fit in?
 
Standardizing for Open Data
Standardizing for Open DataStandardizing for Open Data
Standardizing for Open Data
 
Introduction to the Semantic Web
Introduction to the Semantic WebIntroduction to the Semantic Web
Introduction to the Semantic Web
 
Introduction to Linked Data Platform (LDP)
Introduction to Linked Data Platform (LDP)Introduction to Linked Data Platform (LDP)
Introduction to Linked Data Platform (LDP)
 
Repositories thru the looking glass
Repositories thru the looking glassRepositories thru the looking glass
Repositories thru the looking glass
 
17 applied architectures
17 applied architectures17 applied architectures
17 applied architectures
 

Building REST and Hypermedia APIs with PHP

  • 1. Building REST and Hypermedia APIs with PHP International PHP Conference 2012 Tbilisi, Georgia Wednesday, December 12, 12
  • 2. About me ‣ Ioseb Dzmanashvili ‣ Software Architect at AzRy LLC ‣ Teacher at Caucasus School of Technology ‣ V8 JavaScript engine contributor ‣ Author of uri_template PHP extension ‣ Author of Create-Form and Edit-Form link relation types (being RFCed now). Wednesday, December 12, 12
  • 3. REST Representational State Transfer Wednesday, December 12, 12
  • 4. What is REST? ‣ It’s not a specification ‣ It’s not a design pattern ‣ It’s not... bla bla bla... ‣ It’s an architectural style Wednesday, December 12, 12
  • 5. Architectural Style Definition An architectural style is a coordinated set of architectural constraints that restricts the roles/features of architectural elements and the allowed relationships among those elements within any architecture that conforms to that style. Roy T. Fielding 2000 (quote from dissertation) Wednesday, December 12, 12
  • 6. Architectural Style Definition An architectural style is a named collection of architectural design decisions that: 1) are applicable in a given development context; 2) constrain architectural decision that are specific to a particular system within that context; and 3) elicit beneficial qualities in each resulting system. Richard N. Taylor et al. Wednesday, December 12, 12
  • 7. Analogy Gothic Architecture Wednesday, December 12, 12
  • 8. Analogy Georgian Church Architecture Wednesday, December 12, 12
  • 9. Analogy Industrial Architecture Wednesday, December 12, 12
  • 10. REST Constraints • Client Server: Separation of concerns is the principle behind the client-server constraints. ...Perhaps most significant to the Web, however, is that the separation allows the components to evolve independently. • Stateless: ...communication must be stateless in nature, ...such that each request from client to server must contain all of the information necessary to understand the request, and cannot take advantage of any stored context on the server • Cache: The advantage of adding cache constraints is that they have the potential to partially or completely eliminate some interactions, improving efficiency, scalability, and user-perceived performance by reducing the average latency of a series of interactions. • Layered System: The layered system style allows an architecture to be composed of hierarchical layers by constraining component behavior such that each component cannot "see" beyond the immediate layer with which they are interacting. • Code-On-Demand: (Optional constraint) • Uniform Interface: (To be continued) Wednesday, December 12, 12
  • 11. Uniform Interface By applying the software engineering principle of generality to the component interface, the overall system architecture is simplified and the visibility of interactions is improved. Implementations are decoupled from the services they provide, which encourages independent evolvability. In order to obtain a uniform interface, multiple architectural constraints are needed to guide the behavior of components. REST is defined by four interface constraints: • identification of resources; • manipulation of resources through representations; • self-descriptive messages; and, • hypermedia as the engine of application state. Wednesday, December 12, 12
  • 13. HTTP The Hypertext Transfer Protocol (HTTP) is an application-level protocol for distributed, collaborative, hypertext information systems. Hypertext Transfer Protocol RFC, HTTPbis Wednesday, December 12, 12
  • 14. Hypertext By "hypertext", I mean non-sequential writing - text that branches and allows choices to the reader, best read at an interactive screen. As popularity conceived, this is a series of text chunks connected by links which offer the reader different pathways. Theodor Nelson 1960s Wednesday, December 12, 12
  • 15. Hypertext An intriguing possibility, given a large hypertext database with typed links, is that it allows some degree of automatic analysis. Tim Berners-Lee 1989, CERN (Initial WWW proposal) Wednesday, December 12, 12
  • 16. Hypermedia Hypermedia simply extends the notion of the text in hypertext by including visual information, sound, animation, and other forms of data. George P. Landow Wednesday, December 12, 12
  • 17. Hypermedia Hypermedia system, contains various types of relationships between elements of information. Hypermedia allows these relationships to be instantiated as links which connect the various information elements, so that these links can be used to navigate within the information space. David Lowe, Wendy Hau 1999 Wednesday, December 12, 12
  • 18. Hypermedia Hypermedia: An application which uses associative relationships among information contained within multiple media data for the purpose of facilitating access to, and manipulation of, information encapsulated by the data David Lowe, Wendy Hau 1999 Wednesday, December 12, 12
  • 19. Hypertext When I say hypertext, I mean the simultaneous presentation of information and controls such that the information becomes the affordance through which the user (or automaton) obtains choices and selects actions Roy T. Fielding 2008 Wednesday, December 12, 12
  • 20. Examples of controls? <!-- non visible link --> <link rel="stylesheet" type="text/css" href="styles.css"> <!-- outgoing link --> <a href="/post/1/author" rel="author">John Doe</a> <!-- embed link(transclusion) --> <img src="/logo.jpg" alt="IBM Logo"> <!-- templated link. produces: /search?q=term --> <form method="get" action="/search"> <label>Search: </label><input name="q"> </form> <!-- action link --> <form method="post" action="/posts" type="text/plain"> <textarea>Representational State Transfer</textarea> </form> Wednesday, December 12, 12
  • 22. The Story Hey folks, we’ve created amazing photo sharing service and we have an API! check it out: http://service.org/photos Wednesday, December 12, 12
  • 23. Let’s try it List of Photos GET /photos HTTP/1.1 Host: service.org 1 Request Response 2 HTTP/1.1 200 OK Link: </photos/1>; rel="enclosure"; type="image/jpg"; title= "Egypt", </photos/2>; rel="enclosure"; type="image/jpg"; title="Paris", </photos/3>; rel="enclosure"; type="image/jpg"; title="Tbilisi", </photos/upload-form>; rel="create-form"; title="Upload photo" Wednesday, December 12, 12
  • 24. What’s this Link Header? Quotes from Web Linking spec(RFC5988) • Link is a typed connection between two resources that are identified by Internationalised Resource Identifiers (IRIs) • Links between resources need not be format specific; it can be useful to have typed links that are independent of their serialization. • The relation type of a link is conveyed in the "rel" parameter's value. • The "type" parameter, when present, is a hint indicating what the media type of the result of dereferencing the link should be. Wednesday, December 12, 12
  • 25. Meaning of Relations Quotes from specs • The value "enclosure" signifies a related resource that is potentially large and might require special handling. • The value "create-form" signifies a related resource where a submission form can be obtained. Wednesday, December 12, 12
  • 26. Fetching Photo (Client A) Request GET /photos/1 HTTP/1.1 1 Host: service.org HTTP/1.1 200 OK Content-Type: image/jpeg Response Content-Length: 1000 Cache-Control: private, max-age=0 2 ETag: a32lasdf [PHOTO HERE] GET /photos/1 HTTP/1.1 Request 3 Host: service.org If-None-Match: a32lasdf Response HTTP/1.1 304 Not Modified 4 Wednesday, December 12, 12
  • 27. Client B Deletes Photo Request DELETE /photos/1 HTTP/1.1 1 Host: service.org Response HTTP/1.1 204 No Content 2 Client A Fetches it Again GET /photos/1 HTTP/1.1 Request 3 Host: service.org If-None-Match: a32lasdf HTTP/1.1 404 Not Found Response Content-Type: ... Content-Length: … 4 Link: </photos>; rel="index"; title="Photos", </photos/upload-form>; rel="create-form"; Wednesday, December 12, 12
  • 28. Uploading Photo (Step 1) GET /photos/upload-form HTTP/1.1 Request 1 Host: service.org HTTP/1.1 200 OK Content-Type: application/vnd.forms+json Content-Length: … Response { "method": "POST", 2 "action": "/photos", "content-type": [ "image/jpeg", "image/png", "image/gif", ] } Wednesday, December 12, 12
  • 29. Uploading Photo (Step 2) POST /photos HTTP/1.1 Host: service.org Request Content-Type: image/jpeg 1 Content-Length: 3000 [PHOTO HERE] Response HTTP/1.1 202 Accepted Link: </photos/queue/e3ba8>; rel="monitor" 2 Retry-After: 10 Request GET /photos/queue/e3ba8 HTTP/1.1 3 Host: service.org Response HTTP/1.1 303 See Other Location: /photos/1234 4 Wednesday, December 12, 12
  • 31. Do not use xml or json directly Do not use these: ‣ application/xml ‣ application/json Instead try: ‣ application/atom+xml ‣ application/vnd.collection+json ‣ application/vnd.hal+json ‣ or design your own. Wednesday, December 12, 12
  • 32. Never encode action in URI Wrong: ‣ /some/resource/delete ‣ /some/resource/update Correct: ‣ DELETE /some/resource HTTP/1.1 ‣ POST /some/resource Wednesday, December 12, 12
  • 33. Do not Abuse Protocol Wrong: HTTP/1.1 200 OK Content-Type: application/json Content-Length: … {"error": { "code": 1234, "message": "Page not found" }} Correct: HTTP/1.1 404 Not Found Content-Type: application/json Content-Length: … Wednesday, December 12, 12
  • 34. Do not Use URIs Directly HTTP/1.1 404 Not Found Content-Type: ... Content-Length: … Link: </photos>; rel="index"; title="Photos", </photos/upload-form>; rel="create-form"; HTTP/1.1 404 Not Found Content-Type: ... Content-Length: … Link: </photos>; rel="index"; title="Photos", </photos/upload-form>; rel="create-form"; Wednesday, December 12, 12
  • 35. Do not hardcode control information in your code HTTP/1.1 200 OK Content-Type: application/vnd.forms+json Content-Length: … { "method": "POST", "action": "/photos", "content-type": [ "image/jpeg", HTTP/1.1 200 OK ] Content-Type: application/vnd.forms+json } Content-Length: … { "method": "POST", "action": "/photos", "content-type": [ "image/jpeg", ] } Wednesday, December 12, 12
  • 36. Links in JSON Wednesday, December 12, 12
  • 37. And where is PHP? sorry... i lied :( Wednesday, December 12, 12