Upgrade to Pro — share decks privately, control downloads, hide ads and more …

RESTful Web Services

RESTful Web Services

A textbook introduction to RESTful Web Services, the Web and the HTTP protocol. There has been a lot of hype about REST lately, but very few people understand how to implement RESTful Web Services correctly and how the Web actually works. Presented at ThoughtWorks.

Felipe Dornelas

November 04, 2015
Tweet

More Decks by Felipe Dornelas

Other Decks in Programming

Transcript

  1. a n i n t r o d u c

    t i o n t o RESTFUL WEB SERVICES Felipe Dornelas
  2. AGENDA 2 ▫︎The Internet ▫︎The Web and its Resources ▫︎HTTP

    ▫︎The Resource-Oriented Architecture ▫︎RESTful Web Services
  3. 5

  4. 6

  5. WHAT IS THE WEB? 14 An information system of interlinked

    hypertext documents and resources accessed via the Internet
  6. HYPERTEXT MARKUP LANGUAGE 16 <!doctype html> <html> <head> <title>Example Hypertext

    Document</title> </head> <body> <div> <h1>Example Hypertext Document</h1> <p>This is an example hypertext document to be used for illustrative purposes.</p> <p><a href=“http://example.org”> Example Hyperlink</a></p> </div> </body> </html>
  7. HTTP RESPONSE 21 HTTP/1.1 200 OK Content-Type: text/html Content-Length: 1270

    <!doctype html> <html> <head> <title>Example Domain</title> </head> <body> … </body> </html>
  8. 22

  9. RESOURCES 26 ▫︎Can also represent abstract concepts: ▫︎Employees in a

    enterprise ▫︎Money transfers ▫︎Products in a online store ▫︎Calendar appointments ▫︎User accounts
  10. RESOURCE NAMES 27 ▫︎URN - Uniform Resource Name ▫︎products/54321 ▫︎about-us

    ▫︎articles/web.html ▫︎posts/2015-04-13 ▫︎podcasts/rest.mp3
  11. RESOURCE LOCATORS 28 ▫︎URL - Uniform Resource Locator ▫︎http://example.com/products/54321 ▫︎http://example.com/about-us

    ▫︎http://example.com/articles/web.html ▫︎http://example.com/posts/2015-04-13 ▫︎http://example.com/podcasts/rest.mp3
  12. READING A TEXT RESOURCE 36 HTTP/1.1 200 OK Content-Type: text/plain

    Content-Length: 13 Hello, World! HTTP Response
  13. CREATING A TEXT RESOURCE 37 POST / HTTP/1.1 Host: example.com

    Content-Type: text/plain Hello, Mars! HTTP Request
  14. HTTP CONTENT TYPES 43 ▫︎Determine the type of the HTTP

    payload ▫︎text/html - HTML ▫︎text/plain - Plain Text ▫︎audio/mpeg3 - MP3 files ▫︎application/xml - XML ▫︎…
  15. HTTP STATUS CODES 46 ▫︎Client Error (4xx) ▫︎400 Bad Request

    ▫︎404 Not Found ▫︎409 Conflict ▫︎…
  16. LIST EMPLOYEE RESOURCES 61 HTTP/1.1 200 OK Content-Type: application/xml <employees>

    <employee href="/employees/alice"/> <employee href="/employees/bob"/> <employee href="/employees/eve"/> </employee> HTTP Response
  17. READ EMPLOYEE RESOURCE 63 HTTP/1.1 200 OK Content-Type: application/xml <employee>

    <name>Alice</name> <role>Developer</role> <gender>female</gender> </employee> HTTP Response
  18. CREATE EMPLOYEE RESOURCE 64 POST /employees HTTP/1.1 Host: example.com Content-Type:

    application/xml <employee> <name>John</name> <role>QA</role> <gender>male</gender> </employee> HTTP Request
  19. UPDATE EMPLOYEE RESOURCE 66 PUT /employees/alice HTTP/1.1 Host: example.com Content-Type:

    application/xml <employee> <name>Alice</name> <role>Manager</role> <gender>female</gender> </employee> HTTP Request
  20. ADDRESSABILITY 71 Every interesting piece of information the server can

    provide should be exposed as a resource, and given its own URI
  21. CONNECTEDNESS 83 { "employees": [ "/employees/alice", "/employees/bob", "/employees/eve", ... ]

    "next_page": "/employees?start=10", "create_employee": "/employees" }
  22. UNIFORM INTERFACE 85 ▫︎Create: POST /employees ▫︎Read: GET /employees/alice ▫︎Update:

    PUT /employees/alice ▫︎Delete: DELETE /employees/alice ▫︎List: GET /employees
  23. UNIFORM INTERFACE 86 ▫︎Create: POST /resource ▫︎Read: GET /resource/{name} ▫︎Update:

    PUT /resource/{name} ▫︎Delete: DELETE /resource/{name} ▫︎List: GET /resource
  24. DISCOVERABILITY 101 { "employees": [ "/employees/alice", "/employees/bob", "/employees/eve", ... ]

    "next_page": "/employees?start=10", "create_employee": "/employees" }