SlideShare a Scribd company logo
How to do RESTful
web services right
   Kerry Buckley & Paul Moser

         BT DevCon2

         9 March 2010
w ay
 ne
O How to do RESTful
   web services right
w ay
 ne
O How to do RESTful
   web services right
     (for some value of ‘right’)
What is REST?



REST is an architectural style, not a protocol or a standard. Hence many values of “right” (and
much disagreement).
‘A style of software
              architecture for
          distributed hypermedia
            systems such as the
             World Wide Web’

Not necessarily HTTP, although in practice it generally is.
Not SOAP



Seems to be a common (but wrong) definition.
Constraints
• Client-server
• Stateless
• Cacheable
• Layered system
• Uniform interface
Principles
• Identification of resources
• Manipulation of resources through these
  representations

• Self-descriptive messages
• Hypermedia as the engine of application
  state
Goals
• Scalability of component interactions
• Generality of interfaces
• Independent deployment of components
• Intermediary components to reduce
  latency, enforce security and encapsulate
  legacy systems
Shop example
• List products
• View a product
• Create an order for a number of products
• Cancel order
• Pay for order
XML-RPC
(URI-tunnelling)
GET /api?action=list_products

      GET /api?action=view_product&product_id=123

      GET /api?action=create_order&product_id=123&product_id=456…

      GET/api?action=pay_order&order_id=42&card_number=1234…




Sometimes only a single ‘endpoint’ URI
GET /list_products

     GET /view_product?product_id=123

     GET /create_order?product_id=123&product_id=456…

     GET/pay_order?order_id=42&card_number=1234…




Or one endpoint per method
GET /list_products

      GET /view_product?product_id=123

      POST /create_order?product_id=123&product_id=456…

      POST /pay_order?order_id=42&card_number=1234…




Marginally better without unsafe GETs
POST /pay_order?order_id=42&card_number=1234…

      def pay_order(order_id, card_number) {
        …
      }




Request maps onto method call.
✓Easy to understand
✓Works well for simple procedure calls
✓Simple to implement
✓Interoperable
✗ Brittle
          ✗ Tightly coupled
          ✗ Failure cases require manual handling
          ✗ No metadata


Need to know all the URIs, methods and parameters in advance (out-of-band documentation)
But is it REST?
✗ Identification of resources
✗ Manipulation of resources through these
   representations
✗ Self-descriptive messages
✗ Hypermedia as the engine of application
   state
POX
(plain old XML)
Request

     POST /create_order

     <create_order_request>
      <line>
        <product_id>123</product_line>
        <quantity>1</quantity>
      </line>
      …
     </create_order_request>

     Response

     200 OK

     <create_order_response>
      <status>OK</status>
      <order_id>42</order_id>
     </create_order_response>

Both request and response have XML bodies.
✓Simple to implement
✓Interoperable
✓Allows complex data structures
✗ Tightly coupled
✗ No metadata
✗ Doesn’t use web for robustness
✗ Doesn’t use SOAP for robustness either
But is it REST?
✗ Identification of resources
✗ Manipulation of resources through these
   representations
✗ Self-descriptive messages
✗ Hypermedia as the engine of application
   state
CRUD
GET /products

GET /products/123

POST /orders

PUT /orders/42

DELETE /orders/42

POST /orders/42/payment
Representations
•   Hypermedia              •   Non-hypermedia

    -   XHTML                   -   Generic XML

    -   Atom                    -   YAML

    -   Custom XML schema       -   JSON

                                -   CSV

                                -   etc
✓Makes good use of HTTP
          ✓Uniform interface
          ✓Good for database-style applications


Because each resource has a URI you can use caching etc. Uniform interface: verbs are GET,
POST, PUT and DELETE.
✗ Ignores hypermedia
✗ Tight coupling through URI templates
✗ Not self-describing
But is it REST?
✓ Identification of resources
✓ Manipulation of resources through these
   representations
✗ Self-descriptive messages
✗ Hypermedia as the engine of application
   state
REST
API root
Request

GET /
API root
            Response

            200 OK
            Content-Type: application/vnd.rest-example.store+xml

            <store>
             <link method="get" rel="products" href="http://rest-demo.local/products"/>
             <link method="get" rel="orders" href="http://rest-demo.local/orders"/>
            </store>




Links contain information on what we can do.
View products
             Request

             Get /products




Following the link with a relation of ‘products’ (link rel="products").
View products
             Response

             200 OK
             Content-Type: application/vnd.rest-example.products+xml

             <products>
              <link method="get" rel="latest" href="http://rest-demo.local/products"/>
              <product>
               <link method="get" rel="view" href="http://rest-demo.local/products/1"/>
               <code>A-001</code>
               <description>Tartan Paint</description>
               <price>4.95</price>
              </product>
              …
             </products>




Note link to retrieve the latest version of this resource (eg if you had it cached).
View orders
Request

Get /orders
View orders
            Response

            200 OK
            Content-Type: application/vnd.rest-example.orders+xml

            <orders>
             <link method="get" rel="latest" href="http://rest-demo.local/orders"/>
             <link type="application/vnd.rest-example.order+xml" method="post"
              rel="new" href="http://rest-demo.local/orders"/>
            </orders>




No orders yet, but note link to create a new one.
Place order
            Request

            POST /orders
            Content-Type: application/vnd.rest-example.order+xml

            <order>
             <line>
              <product>http://rest-demo.local/products/1</product>
              <quantity>2</quantity>
             </line>
            </order>




Again following a link, this time posting data of the specified type.
Place order
            Response

            201 Created
            Content-Type: application/vnd.rest-example.order+xml

            <order>
             <link method="get" rel="latest" href="http://rest-demo.local/orders/9"/>
             <link method="delete" rel="cancel" href="http://rest-demo.local/orders/9"/>
             <link type="application/vnd.rest-example.payment-details+xml"
              method="post" rel="pay" href="http://rest-demo.local/orders/9/pay"/>
             <line>
              <product>http://rest-demo.local/products/1</product>
              <quantity>2</quantity>
              <cost>9.90</cost>
             </line>
             <total>9.90</total>
            </order>



Note links to cancel or pay for the order – HATEOAS! The links included would depend on the
allowable state transitions (eg no cancel link for a despatched order).
Clients only need know

• The root URI (‘home page’) of the API
• Definitions for the media types used
 - eg schemas or example documents
• Everything else is just HTTP and links
✓Makes full use of HTTP
           ✓Self-describing
           ✓Loosely coupled


Only need to know entry URI and media types – API can be explored using links. If link URIs
change, well-behaved clients should not break.
✗ More complex to implement
          ✗ Transition links can lead back to RPC-style



Frameworks help. Tempting to just add a bunch of actions as links, using overloaded post.
But is it REST?
✓ Identification of resources
✓ Manipulation of resources through these
   representations
✓ Self-descriptive messages
✓ Hypermedia as the engine of application
   state
This is not the One
                    True Way


As mentioned before, REST is just an architectural style. There are other approaches to
creating RESTful APIs.
An alternative:
‘Web site is your API’
• No separation between web site and API
• Representations are HTML
• Data submitted using HTML forms
• Semantics via microformats
Using HTTP features
Response codes
•   200 OK                  •   400 Bad request

•   201 Created             •   403 Forbidden

•   202 Accepted            •   404 Not found

•   204 No content          •   405 Method not allowed

•   301 Moved permanently   •   409 Conflict

                            •   410 Gone

                            •   etc
Restricting transitions
      Request                 Response


OPTIONS /orders/123             200 OK
   (an open order)      Allow: GET, PUT, DELETE

OPTIONS /orders/42             200 OK
 (a despatched order)         Allow: GET
Restricting transitions
      Request                Response


DELETE /orders/123
                           100 Continue
Expect: 100-Continue

 DELETE /orders/42
                       417 Expectation Failed
Expect: 100-Continue
Prevent race conditions
 Request
 PUT /orders/123
 If-Unmodified-Since: Tue, 9 Mar 2010 11:00:00 GMT
 Response
 200 OK
 or
 412 Precondition Failed
Prevent race conditions
 Request
 GET /orders/123
 Response (partial)
 200 OK
 ETag: 686897696a7c876b7e
 Request
 PUT /orders/123
 If-Match: 686897696a7c876b7e
Security

• HTTP basic
• HTTP digest
• Shared secret (OAuth etc)
• SSL
• Selective encryption
More

• Jim Webber tutorial
      http://tinyurl.com/rest-tutorial
• Restfulie framework (Rails & Java):
      http://tinyurl.com/restfulie

More Related Content

PPTX
What You're Missing With Your Current WAF Provider
Cloudflare
 
PPTX
Real time data quality on Flink
Jaydeep Vishwakarma
 
PPTX
Windows Azure Storage – Architecture View
Chaowlert Chaisrichalermpol
 
PPTX
Digital Forensics best practices with the use of open source tools and admiss...
Sagar Rahurkar
 
PPTX
Top frontend web development tools
Benji Harrison
 
PPTX
Burp suite
SOURABH DESHMUKH
 
PPTX
Zed attack proxy [ What is ZAP(Zed Attack Proxy)? ]
raj upadhyay
 
PPTX
A presentation on Phishing
Creative Technology
 
What You're Missing With Your Current WAF Provider
Cloudflare
 
Real time data quality on Flink
Jaydeep Vishwakarma
 
Windows Azure Storage – Architecture View
Chaowlert Chaisrichalermpol
 
Digital Forensics best practices with the use of open source tools and admiss...
Sagar Rahurkar
 
Top frontend web development tools
Benji Harrison
 
Burp suite
SOURABH DESHMUKH
 
Zed attack proxy [ What is ZAP(Zed Attack Proxy)? ]
raj upadhyay
 
A presentation on Phishing
Creative Technology
 

What's hot (20)

PPT
Phishing
oitaoming
 
PDF
Events on the outside, on the inside and at the core (jfokus jfokus2016)
Chris Richardson
 
PDF
Service discovery with Eureka and Spring Cloud
Marcelo Serpa
 
PDF
PostgreSQL High Availability in a Containerized World
Jignesh Shah
 
PPTX
Security Code Review 101
Paul Ionescu
 
PPTX
SSRF exploit the trust relationship
n|u - The Open Security Community
 
PPTX
Cybersecurity and the DarkNet
James Bollen
 
PPT
Darkweb
CHANDRESH PAL
 
PPTX
Bulud texnologiyaları
99Fidan
 
PDF
Capacity Planning Your Kafka Cluster | Jason Bell, Digitalis
HostedbyConfluent
 
PDF
Building Advanced XSS Vectors
Rodolfo Assis (Brute)
 
PPTX
A2 - broken authentication and session management(OWASP thailand chapter Apri...
Noppadol Songsakaew
 
PPTX
SQL injection
Raj Parmar
 
PDF
From Mainframe to Microservice: An Introduction to Distributed Systems
Tyler Treat
 
PPTX
Not Your Mother's Kafka - Deep Dive into Confluent Cloud Infrastructure | Gwe...
HostedbyConfluent
 
PDF
How to break SAML if I have paws?
GreenD0g
 
PDF
The Dual write problem
Jeppe Cramon
 
PPTX
Kafka + Uber- The World’s Realtime Transit Infrastructure, Aaron Schildkrout
confluent
 
PDF
How to Spot and Combat a Phishing Attack - Cyber Security Webinar | ControlScan
ControlScan, Inc.
 
PPTX
Internet security
Tapan Khilar
 
Phishing
oitaoming
 
Events on the outside, on the inside and at the core (jfokus jfokus2016)
Chris Richardson
 
Service discovery with Eureka and Spring Cloud
Marcelo Serpa
 
PostgreSQL High Availability in a Containerized World
Jignesh Shah
 
Security Code Review 101
Paul Ionescu
 
SSRF exploit the trust relationship
n|u - The Open Security Community
 
Cybersecurity and the DarkNet
James Bollen
 
Darkweb
CHANDRESH PAL
 
Bulud texnologiyaları
99Fidan
 
Capacity Planning Your Kafka Cluster | Jason Bell, Digitalis
HostedbyConfluent
 
Building Advanced XSS Vectors
Rodolfo Assis (Brute)
 
A2 - broken authentication and session management(OWASP thailand chapter Apri...
Noppadol Songsakaew
 
SQL injection
Raj Parmar
 
From Mainframe to Microservice: An Introduction to Distributed Systems
Tyler Treat
 
Not Your Mother's Kafka - Deep Dive into Confluent Cloud Infrastructure | Gwe...
HostedbyConfluent
 
How to break SAML if I have paws?
GreenD0g
 
The Dual write problem
Jeppe Cramon
 
Kafka + Uber- The World’s Realtime Transit Infrastructure, Aaron Schildkrout
confluent
 
How to Spot and Combat a Phishing Attack - Cyber Security Webinar | ControlScan
ControlScan, Inc.
 
Internet security
Tapan Khilar
 
Ad

Similar to Doing REST Right (20)

PPTX
Rest with Java EE 6 , Security , Backbone.js
Carol McDonald
 
PPTX
Rest
Carol McDonald
 
PDF
Rest web services
Paulo Gandra de Sousa
 
PDF
RESTful web
Alvin Qi
 
PPT
RESTful SOA - 中科院暑期讲座
Li Yi
 
PDF
Ws rest
patriknw
 
PDF
JAX-RS JavaOne Hyderabad, India 2011
Shreedhar Ganapathy
 
PPTX
RESTful for opentravel.org by HP
Roni Schuetz
 
PPTX
RESTful Services
Jason Gerard
 
PDF
Restful web-services
rporwal
 
PPTX
A Deep Dive into RESTful API Design Part 2
VivekKrishna34
 
PPTX
RESTful APIs
Adi Challa
 
PDF
Writing RESTful Web Services
Paul Boocock
 
PPTX
rest-api-basics.pptx
FikiRieza2
 
KEY
Designing a RESTful web service
Filip Blondeel
 
ODP
RESTful Web Services with JAX-RS
Carol McDonald
 
PPT
ReST-ful Resource Management
Joe Davis
 
PPTX
Rest APIs Training
Shekhar Kumar
 
PPTX
rest-api-basics.pptx
AgungSutikno1
 
PDF
Designing RESTful APIs
anandology
 
Rest with Java EE 6 , Security , Backbone.js
Carol McDonald
 
Rest web services
Paulo Gandra de Sousa
 
RESTful web
Alvin Qi
 
RESTful SOA - 中科院暑期讲座
Li Yi
 
Ws rest
patriknw
 
JAX-RS JavaOne Hyderabad, India 2011
Shreedhar Ganapathy
 
RESTful for opentravel.org by HP
Roni Schuetz
 
RESTful Services
Jason Gerard
 
Restful web-services
rporwal
 
A Deep Dive into RESTful API Design Part 2
VivekKrishna34
 
RESTful APIs
Adi Challa
 
Writing RESTful Web Services
Paul Boocock
 
rest-api-basics.pptx
FikiRieza2
 
Designing a RESTful web service
Filip Blondeel
 
RESTful Web Services with JAX-RS
Carol McDonald
 
ReST-ful Resource Management
Joe Davis
 
Rest APIs Training
Shekhar Kumar
 
rest-api-basics.pptx
AgungSutikno1
 
Designing RESTful APIs
anandology
 
Ad

More from Kerry Buckley (20)

PDF
Jasmine
Kerry Buckley
 
PDF
Testing http calls with Webmock and VCR
Kerry Buckley
 
PDF
BDD with cucumber
Kerry Buckley
 
KEY
Ruby nooks & crannies
Kerry Buckley
 
KEY
TDD refresher
Kerry Buckley
 
KEY
Javasccript MV* frameworks
Kerry Buckley
 
KEY
Tdd for BT E2E test community
Kerry Buckley
 
PDF
7li7w devcon5
Kerry Buckley
 
PDF
What I learned from Seven Languages in Seven Weeks (IPRUG)
Kerry Buckley
 
KEY
Functional ruby
Kerry Buckley
 
KEY
Adastral Park code retreat introduction
Kerry Buckley
 
KEY
MongoMapper lightning talk
Kerry Buckley
 
KEY
Ruby
Kerry Buckley
 
KEY
Cloud
Kerry Buckley
 
PDF
The secret life of bees
Kerry Buckley
 
PDF
Background processing
Kerry Buckley
 
PDF
Katas, Contests and Coding Dojos
Kerry Buckley
 
PDF
Rack
Kerry Buckley
 
PPT
Kanban and Iterationless Working
Kerry Buckley
 
PPT
Software Development Trends
Kerry Buckley
 
Jasmine
Kerry Buckley
 
Testing http calls with Webmock and VCR
Kerry Buckley
 
BDD with cucumber
Kerry Buckley
 
Ruby nooks & crannies
Kerry Buckley
 
TDD refresher
Kerry Buckley
 
Javasccript MV* frameworks
Kerry Buckley
 
Tdd for BT E2E test community
Kerry Buckley
 
7li7w devcon5
Kerry Buckley
 
What I learned from Seven Languages in Seven Weeks (IPRUG)
Kerry Buckley
 
Functional ruby
Kerry Buckley
 
Adastral Park code retreat introduction
Kerry Buckley
 
MongoMapper lightning talk
Kerry Buckley
 
The secret life of bees
Kerry Buckley
 
Background processing
Kerry Buckley
 
Katas, Contests and Coding Dojos
Kerry Buckley
 
Kanban and Iterationless Working
Kerry Buckley
 
Software Development Trends
Kerry Buckley
 

Recently uploaded (20)

PPTX
OA presentation.pptx OA presentation.pptx
pateldhruv002338
 
PPTX
cloud computing vai.pptx for the project
vaibhavdobariyal79
 
PDF
Using Anchore and DefectDojo to Stand Up Your DevSecOps Function
Anchore
 
PDF
How Open Source Changed My Career by abdelrahman ismail
a0m0rajab1
 
PDF
How ETL Control Logic Keeps Your Pipelines Safe and Reliable.pdf
Stryv Solutions Pvt. Ltd.
 
PDF
Oracle AI Vector Search- Getting Started and what's new in 2025- AIOUG Yatra ...
Sandesh Rao
 
PDF
Structs to JSON: How Go Powers REST APIs
Emily Achieng
 
PPTX
Introduction to Flutter by Ayush Desai.pptx
ayushdesai204
 
PPTX
AI and Robotics for Human Well-being.pptx
JAYMIN SUTHAR
 
PDF
Automating ArcGIS Content Discovery with FME: A Real World Use Case
Safe Software
 
PPTX
Applied-Statistics-Mastering-Data-Driven-Decisions.pptx
parmaryashparmaryash
 
PDF
The Future of Artificial Intelligence (AI)
Mukul
 
PDF
CIFDAQ's Market Wrap : Bears Back in Control?
CIFDAQ
 
PDF
Responsible AI and AI Ethics - By Sylvester Ebhonu
Sylvester Ebhonu
 
PDF
Research-Fundamentals-and-Topic-Development.pdf
ayesha butalia
 
PDF
AI Unleashed - Shaping the Future -Starting Today - AIOUG Yatra 2025 - For Co...
Sandesh Rao
 
PDF
Get More from Fiori Automation - What’s New, What Works, and What’s Next.pdf
Precisely
 
PDF
Presentation about Hardware and Software in Computer
snehamodhawadiya
 
PPTX
The Future of AI & Machine Learning.pptx
pritsen4700
 
PPTX
Agile Chennai 18-19 July 2025 Ideathon | AI Powered Microfinance Literacy Gui...
AgileNetwork
 
OA presentation.pptx OA presentation.pptx
pateldhruv002338
 
cloud computing vai.pptx for the project
vaibhavdobariyal79
 
Using Anchore and DefectDojo to Stand Up Your DevSecOps Function
Anchore
 
How Open Source Changed My Career by abdelrahman ismail
a0m0rajab1
 
How ETL Control Logic Keeps Your Pipelines Safe and Reliable.pdf
Stryv Solutions Pvt. Ltd.
 
Oracle AI Vector Search- Getting Started and what's new in 2025- AIOUG Yatra ...
Sandesh Rao
 
Structs to JSON: How Go Powers REST APIs
Emily Achieng
 
Introduction to Flutter by Ayush Desai.pptx
ayushdesai204
 
AI and Robotics for Human Well-being.pptx
JAYMIN SUTHAR
 
Automating ArcGIS Content Discovery with FME: A Real World Use Case
Safe Software
 
Applied-Statistics-Mastering-Data-Driven-Decisions.pptx
parmaryashparmaryash
 
The Future of Artificial Intelligence (AI)
Mukul
 
CIFDAQ's Market Wrap : Bears Back in Control?
CIFDAQ
 
Responsible AI and AI Ethics - By Sylvester Ebhonu
Sylvester Ebhonu
 
Research-Fundamentals-and-Topic-Development.pdf
ayesha butalia
 
AI Unleashed - Shaping the Future -Starting Today - AIOUG Yatra 2025 - For Co...
Sandesh Rao
 
Get More from Fiori Automation - What’s New, What Works, and What’s Next.pdf
Precisely
 
Presentation about Hardware and Software in Computer
snehamodhawadiya
 
The Future of AI & Machine Learning.pptx
pritsen4700
 
Agile Chennai 18-19 July 2025 Ideathon | AI Powered Microfinance Literacy Gui...
AgileNetwork
 

Doing REST Right

  • 1. How to do RESTful web services right Kerry Buckley & Paul Moser BT DevCon2 9 March 2010
  • 2. w ay ne O How to do RESTful web services right
  • 3. w ay ne O How to do RESTful web services right (for some value of ‘right’)
  • 4. What is REST? REST is an architectural style, not a protocol or a standard. Hence many values of “right” (and much disagreement).
  • 5. ‘A style of software architecture for distributed hypermedia systems such as the World Wide Web’ Not necessarily HTTP, although in practice it generally is.
  • 6. Not SOAP Seems to be a common (but wrong) definition.
  • 8. • Client-server • Stateless • Cacheable • Layered system • Uniform interface
  • 10. • Identification of resources • Manipulation of resources through these representations • Self-descriptive messages • Hypermedia as the engine of application state
  • 11. Goals
  • 12. • Scalability of component interactions • Generality of interfaces • Independent deployment of components • Intermediary components to reduce latency, enforce security and encapsulate legacy systems
  • 14. • List products • View a product • Create an order for a number of products • Cancel order • Pay for order
  • 16. GET /api?action=list_products GET /api?action=view_product&product_id=123 GET /api?action=create_order&product_id=123&product_id=456… GET/api?action=pay_order&order_id=42&card_number=1234… Sometimes only a single ‘endpoint’ URI
  • 17. GET /list_products GET /view_product?product_id=123 GET /create_order?product_id=123&product_id=456… GET/pay_order?order_id=42&card_number=1234… Or one endpoint per method
  • 18. GET /list_products GET /view_product?product_id=123 POST /create_order?product_id=123&product_id=456… POST /pay_order?order_id=42&card_number=1234… Marginally better without unsafe GETs
  • 19. POST /pay_order?order_id=42&card_number=1234… def pay_order(order_id, card_number) { … } Request maps onto method call.
  • 20. ✓Easy to understand ✓Works well for simple procedure calls ✓Simple to implement ✓Interoperable
  • 21. ✗ Brittle ✗ Tightly coupled ✗ Failure cases require manual handling ✗ No metadata Need to know all the URIs, methods and parameters in advance (out-of-band documentation)
  • 22. But is it REST? ✗ Identification of resources ✗ Manipulation of resources through these representations ✗ Self-descriptive messages ✗ Hypermedia as the engine of application state
  • 24. Request POST /create_order <create_order_request> <line> <product_id>123</product_line> <quantity>1</quantity> </line> … </create_order_request> Response 200 OK <create_order_response> <status>OK</status> <order_id>42</order_id> </create_order_response> Both request and response have XML bodies.
  • 26. ✗ Tightly coupled ✗ No metadata ✗ Doesn’t use web for robustness ✗ Doesn’t use SOAP for robustness either
  • 27. But is it REST? ✗ Identification of resources ✗ Manipulation of resources through these representations ✗ Self-descriptive messages ✗ Hypermedia as the engine of application state
  • 28. CRUD
  • 29. GET /products GET /products/123 POST /orders PUT /orders/42 DELETE /orders/42 POST /orders/42/payment
  • 30. Representations • Hypermedia • Non-hypermedia - XHTML - Generic XML - Atom - YAML - Custom XML schema - JSON - CSV - etc
  • 31. ✓Makes good use of HTTP ✓Uniform interface ✓Good for database-style applications Because each resource has a URI you can use caching etc. Uniform interface: verbs are GET, POST, PUT and DELETE.
  • 32. ✗ Ignores hypermedia ✗ Tight coupling through URI templates ✗ Not self-describing
  • 33. But is it REST? ✓ Identification of resources ✓ Manipulation of resources through these representations ✗ Self-descriptive messages ✗ Hypermedia as the engine of application state
  • 34. REST
  • 36. API root Response 200 OK Content-Type: application/vnd.rest-example.store+xml <store> <link method="get" rel="products" href="http://rest-demo.local/products"/> <link method="get" rel="orders" href="http://rest-demo.local/orders"/> </store> Links contain information on what we can do.
  • 37. View products Request Get /products Following the link with a relation of ‘products’ (link rel="products").
  • 38. View products Response 200 OK Content-Type: application/vnd.rest-example.products+xml <products> <link method="get" rel="latest" href="http://rest-demo.local/products"/> <product> <link method="get" rel="view" href="http://rest-demo.local/products/1"/> <code>A-001</code> <description>Tartan Paint</description> <price>4.95</price> </product> … </products> Note link to retrieve the latest version of this resource (eg if you had it cached).
  • 40. View orders Response 200 OK Content-Type: application/vnd.rest-example.orders+xml <orders> <link method="get" rel="latest" href="http://rest-demo.local/orders"/> <link type="application/vnd.rest-example.order+xml" method="post" rel="new" href="http://rest-demo.local/orders"/> </orders> No orders yet, but note link to create a new one.
  • 41. Place order Request POST /orders Content-Type: application/vnd.rest-example.order+xml <order> <line> <product>http://rest-demo.local/products/1</product> <quantity>2</quantity> </line> </order> Again following a link, this time posting data of the specified type.
  • 42. Place order Response 201 Created Content-Type: application/vnd.rest-example.order+xml <order> <link method="get" rel="latest" href="http://rest-demo.local/orders/9"/> <link method="delete" rel="cancel" href="http://rest-demo.local/orders/9"/> <link type="application/vnd.rest-example.payment-details+xml" method="post" rel="pay" href="http://rest-demo.local/orders/9/pay"/> <line> <product>http://rest-demo.local/products/1</product> <quantity>2</quantity> <cost>9.90</cost> </line> <total>9.90</total> </order> Note links to cancel or pay for the order – HATEOAS! The links included would depend on the allowable state transitions (eg no cancel link for a despatched order).
  • 43. Clients only need know • The root URI (‘home page’) of the API • Definitions for the media types used - eg schemas or example documents • Everything else is just HTTP and links
  • 44. ✓Makes full use of HTTP ✓Self-describing ✓Loosely coupled Only need to know entry URI and media types – API can be explored using links. If link URIs change, well-behaved clients should not break.
  • 45. ✗ More complex to implement ✗ Transition links can lead back to RPC-style Frameworks help. Tempting to just add a bunch of actions as links, using overloaded post.
  • 46. But is it REST? ✓ Identification of resources ✓ Manipulation of resources through these representations ✓ Self-descriptive messages ✓ Hypermedia as the engine of application state
  • 47. This is not the One True Way As mentioned before, REST is just an architectural style. There are other approaches to creating RESTful APIs.
  • 48. An alternative: ‘Web site is your API’ • No separation between web site and API • Representations are HTML • Data submitted using HTML forms • Semantics via microformats
  • 50. Response codes • 200 OK • 400 Bad request • 201 Created • 403 Forbidden • 202 Accepted • 404 Not found • 204 No content • 405 Method not allowed • 301 Moved permanently • 409 Conflict • 410 Gone • etc
  • 51. Restricting transitions Request Response OPTIONS /orders/123 200 OK (an open order) Allow: GET, PUT, DELETE OPTIONS /orders/42 200 OK (a despatched order) Allow: GET
  • 52. Restricting transitions Request Response DELETE /orders/123 100 Continue Expect: 100-Continue DELETE /orders/42 417 Expectation Failed Expect: 100-Continue
  • 53. Prevent race conditions Request PUT /orders/123 If-Unmodified-Since: Tue, 9 Mar 2010 11:00:00 GMT Response 200 OK or 412 Precondition Failed
  • 54. Prevent race conditions Request GET /orders/123 Response (partial) 200 OK ETag: 686897696a7c876b7e Request PUT /orders/123 If-Match: 686897696a7c876b7e
  • 55. Security • HTTP basic • HTTP digest • Shared secret (OAuth etc) • SSL • Selective encryption
  • 56. More • Jim Webber tutorial http://tinyurl.com/rest-tutorial • Restfulie framework (Rails & Java): http://tinyurl.com/restfulie