INFO/CS(4302( Web(Informaon(Systems - Cornell University · INFO/CS(4302(Web(Informaon(Systems...

61
INFO/CS 4302 Web Informa6on Systems FT 2012 Week 7: RESTful Webservice APIs Bernhard Haslhofer

Transcript of INFO/CS(4302( Web(Informaon(Systems - Cornell University · INFO/CS(4302(Web(Informaon(Systems...

Page 1: INFO/CS(4302( Web(Informaon(Systems - Cornell University · INFO/CS(4302(Web(Informaon(Systems (FT2012(Week(7:(RESTful(Webservice(APIs((H(Bernhard(Haslhofer(H(

INFO/CS  4302  Web  Informa6on  Systems  

FT  2012  Week  7:  RESTful  Webservice  APIs  

 -­‐  Bernhard  Haslhofer  -­‐    

Page 2: INFO/CS(4302( Web(Informaon(Systems - Cornell University · INFO/CS(4302(Web(Informaon(Systems (FT2012(Week(7:(RESTful(Webservice(APIs((H(Bernhard(Haslhofer(H(

2  

Page 3: INFO/CS(4302( Web(Informaon(Systems - Cornell University · INFO/CS(4302(Web(Informaon(Systems (FT2012(Week(7:(RESTful(Webservice(APIs((H(Bernhard(Haslhofer(H(

3  

Page 4: INFO/CS(4302( Web(Informaon(Systems - Cornell University · INFO/CS(4302(Web(Informaon(Systems (FT2012(Week(7:(RESTful(Webservice(APIs((H(Bernhard(Haslhofer(H(

4  

Page 5: INFO/CS(4302( Web(Informaon(Systems - Cornell University · INFO/CS(4302(Web(Informaon(Systems (FT2012(Week(7:(RESTful(Webservice(APIs((H(Bernhard(Haslhofer(H(

Source:  hMp://www.blogperfume.com/new-­‐27-­‐circular-­‐social-­‐media-­‐icons-­‐in-­‐3-­‐sizes/    

5  

Page 6: INFO/CS(4302( Web(Informaon(Systems - Cornell University · INFO/CS(4302(Web(Informaon(Systems (FT2012(Week(7:(RESTful(Webservice(APIs((H(Bernhard(Haslhofer(H(

Plan  for  today...  •  Recap  -­‐  Web  Fundamentals  

•  APIs,  Web  Services  

•  Group  Brainstorming  

•  RESTful  APIs  –  Architectural  principles  

•  Ques6ons,  Housekeeping,  ...  

6  

Page 7: INFO/CS(4302( Web(Informaon(Systems - Cornell University · INFO/CS(4302(Web(Informaon(Systems (FT2012(Week(7:(RESTful(Webservice(APIs((H(Bernhard(Haslhofer(H(

RECAP  –  WEB  FUNDAMENTALS  

7  

Page 8: INFO/CS(4302( Web(Informaon(Systems - Cornell University · INFO/CS(4302(Web(Informaon(Systems (FT2012(Week(7:(RESTful(Webservice(APIs((H(Bernhard(Haslhofer(H(

Web  Fundamentals  

   

Internet  ≠  World  Wide  Web  

8  

Page 9: INFO/CS(4302( Web(Informaon(Systems - Cornell University · INFO/CS(4302(Web(Informaon(Systems (FT2012(Week(7:(RESTful(Webservice(APIs((H(Bernhard(Haslhofer(H(

Web  Fundamentals  

•  Key  Architectural  Components  

–  Iden6fica6on:  ???  –  Interac6on:  ???  – Standardized  Document  Formats:  ???,  ???,  ???  

9  

Page 10: INFO/CS(4302( Web(Informaon(Systems - Cornell University · INFO/CS(4302(Web(Informaon(Systems (FT2012(Week(7:(RESTful(Webservice(APIs((H(Bernhard(Haslhofer(H(

Web  Fundamentals  

•  Key  Architectural  Components  

–  Iden6fica6on:  URI  –  Interac6on:  HTTP  – Standardized  Document  Formats:  HTML,  XML,  JSON,  etc.  

10  

Page 11: INFO/CS(4302( Web(Informaon(Systems - Cornell University · INFO/CS(4302(Web(Informaon(Systems (FT2012(Week(7:(RESTful(Webservice(APIs((H(Bernhard(Haslhofer(H(

Principle  ‘Orthogonal  Specifica6ons’  

URI/  Ide

n6fica6

on  

hMp/  Interac6on  

11  

Page 12: INFO/CS(4302( Web(Informaon(Systems - Cornell University · INFO/CS(4302(Web(Informaon(Systems (FT2012(Week(7:(RESTful(Webservice(APIs((H(Bernhard(Haslhofer(H(

URIs  /  Resources  

•  URIs  iden6fy  interes6ng  things  – documents  on  the  Web  –  relevant  aspects  of  a  data  set  

•  HTTP  URIs  name  and  address  resources  in  Web-­‐based  systems  – a  URI  names  and  iden6fies  one  resource  – a  resource  can  have  more  than  one  name  

•  hMp://foo.com/sogware/latest  •  hMp://foo.com/sogware/v1.4  

12  

Page 13: INFO/CS(4302( Web(Informaon(Systems - Cornell University · INFO/CS(4302(Web(Informaon(Systems (FT2012(Week(7:(RESTful(Webservice(APIs((H(Bernhard(Haslhofer(H(

Resource  Representa6on  

•  A  resource  can  have  several  representa6ons  

•  Representa6ons  can  be  in  any  format  –  HTML  –  XML  –  JSON  –  …  

URI

Resource

RepresentationPlain Texttext/plain

http://example.com/someURI

RepresentationHTML

text/html

RepresentationJSON

text/json

13  

Page 14: INFO/CS(4302( Web(Informaon(Systems - Cornell University · INFO/CS(4302(Web(Informaon(Systems (FT2012(Week(7:(RESTful(Webservice(APIs((H(Bernhard(Haslhofer(H(

Interac6ng  with  Resources  •  We  deal  with  resource  representa6ons  

–  not  the  resources  themselves  (pass  by  value)  –  representa6ons  can  be  in  any  format  (defined  by  media-­‐type)  

•  Each  resource  implements  a  standard  uniform  interface  (HTTP)  –  a  small  set  of  verbs  applied  to  a  large  set  of  nouns  –  verbs  are  universal  and  not  invented  on  a  per-­‐applica6on  basis  

Client Server

LogicalResources

PhysicalResources

JSON

Resource Representations

Uniform Interface

14  

Page 15: INFO/CS(4302( Web(Informaon(Systems - Cornell University · INFO/CS(4302(Web(Informaon(Systems (FT2012(Week(7:(RESTful(Webservice(APIs((H(Bernhard(Haslhofer(H(

Document/Data  Formats  

HTML,  XHTML,  

...  

XML,  JSON,  ...  

Transport  and  store  data  Display  data  

15  

Page 16: INFO/CS(4302( Web(Informaon(Systems - Cornell University · INFO/CS(4302(Web(Informaon(Systems (FT2012(Week(7:(RESTful(Webservice(APIs((H(Bernhard(Haslhofer(H(

APIS,  WEB  SERVICES  

16  

Page 17: INFO/CS(4302( Web(Informaon(Systems - Cornell University · INFO/CS(4302(Web(Informaon(Systems (FT2012(Week(7:(RESTful(Webservice(APIs((H(Bernhard(Haslhofer(H(

APIs  

What  is  an  API?  and  

Why  do  we  need  APIs?  

17  

Page 18: INFO/CS(4302( Web(Informaon(Systems - Cornell University · INFO/CS(4302(Web(Informaon(Systems (FT2012(Week(7:(RESTful(Webservice(APIs((H(Bernhard(Haslhofer(H(

(Web)  APIs  

•  Applica6on  Programming  Interface  •  Specifies  how  sogware  components  communicate  with  each  other  – e.g.,  Java  API,  3rd  party  library  APIs  – usually  come  with  documenta6on,  howtos  

•  Web  API:  specify  how  applica6ons  communicate  with  other  over  the  Web  (HTTP,  URI,  XML,  etc.)  

18  

Page 19: INFO/CS(4302( Web(Informaon(Systems - Cornell University · INFO/CS(4302(Web(Informaon(Systems (FT2012(Week(7:(RESTful(Webservice(APIs((H(Bernhard(Haslhofer(H(

Web  Services  

•  Example  opera6ons:  – Publish  image  on  Flickr  – Order  a  book  at  Amazon  – Post  a  message  on  your  friend’s  Facebook  wall  – Update  user  photo  on  foursquare  

Web

Application A Application B

A  

API  

19  

Page 20: INFO/CS(4302( Web(Informaon(Systems - Cornell University · INFO/CS(4302(Web(Informaon(Systems (FT2012(Week(7:(RESTful(Webservice(APIs((H(Bernhard(Haslhofer(H(

Web  Services  

•  “Web  Services”  ≅  “Web  APIs”  

•  Build  on  the  design  principles  and  architectural  components  of  the  Web  

•  Provide  certain  opera6ons  

•  Exchange  structured  data  in  standard  formats  (JSON,  XML,  etc)  

20  

Page 21: INFO/CS(4302( Web(Informaon(Systems - Cornell University · INFO/CS(4302(Web(Informaon(Systems (FT2012(Week(7:(RESTful(Webservice(APIs((H(Bernhard(Haslhofer(H(

GROUP  BRAINSTORMING  

21  

Page 22: INFO/CS(4302( Web(Informaon(Systems - Cornell University · INFO/CS(4302(Web(Informaon(Systems (FT2012(Week(7:(RESTful(Webservice(APIs((H(Bernhard(Haslhofer(H(

Instruc6ons  

•  Form  groups  of  5  •  10  min:  – discuss  known  or  possible  Web  API  opera6ons  (func6ons)  

– collect  opera6ons  in  the  form:  •  [verb][noun]  at  [service]  

– one  person  per  group  should  write  them  down  at:  hMp://bit.ly/info4302-­‐api-­‐brainstorming  

22  

Page 23: INFO/CS(4302( Web(Informaon(Systems - Cornell University · INFO/CS(4302(Web(Informaon(Systems (FT2012(Week(7:(RESTful(Webservice(APIs((H(Bernhard(Haslhofer(H(

RESTFUL  APIS  –  ARCHITECTURAL  PRINCIPLES  

23  

Page 24: INFO/CS(4302( Web(Informaon(Systems - Cornell University · INFO/CS(4302(Web(Informaon(Systems (FT2012(Week(7:(RESTful(Webservice(APIs((H(Bernhard(Haslhofer(H(

24  

Page 25: INFO/CS(4302( Web(Informaon(Systems - Cornell University · INFO/CS(4302(Web(Informaon(Systems (FT2012(Week(7:(RESTful(Webservice(APIs((H(Bernhard(Haslhofer(H(

RESTful  Webservices  

•  REST  =  Representa6onal  State  Transfer  – Based  on  Chapter  5  of  Roy  Fielding’s  2000  PhD  thesis  (it  is  in  your  reading  list!)  

•  An  architectural  style  for  building  loosely  coupled  systems  

•  The  Web  itself  is  an  instance  of  that  style  •  Web  Services  can  be  built  on  top  of  it  

25  

Page 26: INFO/CS(4302( Web(Informaon(Systems - Cornell University · INFO/CS(4302(Web(Informaon(Systems (FT2012(Week(7:(RESTful(Webservice(APIs((H(Bernhard(Haslhofer(H(

The  Resource-­‐Oriented  Architecture  

•  A  set  of  design  principles  for  building  RESTful  Web  Services  – Addressability  – Uniform  interface  – Connectedness  – Statelessness  

26  

Page 27: INFO/CS(4302( Web(Informaon(Systems - Cornell University · INFO/CS(4302(Web(Informaon(Systems (FT2012(Week(7:(RESTful(Webservice(APIs((H(Bernhard(Haslhofer(H(

Addressability  

•  An  addressable  applica6on  – exposes  the  interes6ng  aspects  of  its  dataset  as  resources  

– exposes  a  URI  for  every  piece  of  informa6on  it  might  serve  

– which  is  usually  an  infinite  number  of  URIs  

27  

Page 28: INFO/CS(4302( Web(Informaon(Systems - Cornell University · INFO/CS(4302(Web(Informaon(Systems (FT2012(Week(7:(RESTful(Webservice(APIs((H(Bernhard(Haslhofer(H(

Addressability  

•  A  resource  –  is  anything  that  is  important  enough  to  be  referenced  as  a  thing  in  itself  

– usually  something  •  you  want  to  serve  informa6on  about  •  that  can  be  represented  as  a  stream  of  bits  

–  actors  – movies  

– a  resource  must  have  at  least  one  name  (URI)  

28  

Page 29: INFO/CS(4302( Web(Informaon(Systems - Cornell University · INFO/CS(4302(Web(Informaon(Systems (FT2012(Week(7:(RESTful(Webservice(APIs((H(Bernhard(Haslhofer(H(

Addressability  

•  Resource  names  (URIs)  –  the  URI  is  the  name  and  address  of  a  resource  – a  resource’s  URI  should  be  descrip6ve  

http://example.com/movies!!instead of!!http://example.com/overview.php?list=all,type=movie!!

29  

Page 30: INFO/CS(4302( Web(Informaon(Systems - Cornell University · INFO/CS(4302(Web(Informaon(Systems (FT2012(Week(7:(RESTful(Webservice(APIs((H(Bernhard(Haslhofer(H(

The  Resource-­‐Oriented  Architecture  

•  A  set  of  design  principles  for  building  RESTful  Web  Services  – Addressability  – Uniform  interface  – Connectedness  – Statelessness  

30  

Page 31: INFO/CS(4302( Web(Informaon(Systems - Cornell University · INFO/CS(4302(Web(Informaon(Systems (FT2012(Week(7:(RESTful(Webservice(APIs((H(Bernhard(Haslhofer(H(

Uniform  Interface  

•  The  same  set  of  opera6ons  applies  to  everything  (every  resource)  

•  A  small  set  of  verbs  (methods)  applied  to  a  large  set  of  nouns  (resources)  – verbs  are  universal  and  not  invented  on  a  per-­‐applica6on  base  

•  Natural  language  works  in  the  same  way  (new  verbs  rarely  enter  language)  

31  

Page 32: INFO/CS(4302( Web(Informaon(Systems - Cornell University · INFO/CS(4302(Web(Informaon(Systems (FT2012(Week(7:(RESTful(Webservice(APIs((H(Bernhard(Haslhofer(H(

Uniform  Interface  

•  HTTP  defines  a  small  set  of  verbs  (methods)  for  ac6ng  on  URI-­‐iden6fied  resources  

Which  methods  (verbs)  are  defined  in  HTTP?  

32  

Page 33: INFO/CS(4302( Web(Informaon(Systems - Cornell University · INFO/CS(4302(Web(Informaon(Systems (FT2012(Week(7:(RESTful(Webservice(APIs((H(Bernhard(Haslhofer(H(

Uniform  Interface  

•  RESTful  Web  Services  use  HTTP  to  its  full  extent  – Methods:  GET,  POST,  PUT,  DELETE,  (...)  – Request  headers:  Authoriza6on,  Content-­‐Type,  Last-­‐Modified  

– Response  Codes:  200  OK,  304  Not  Modified,  401  Unauthorized,  500  Internal  Server  Error  

– Body:  an  envelope  for  data  to  be  transported  from  A  to  B  

33  

Page 34: INFO/CS(4302( Web(Informaon(Systems - Cornell University · INFO/CS(4302(Web(Informaon(Systems (FT2012(Week(7:(RESTful(Webservice(APIs((H(Bernhard(Haslhofer(H(

Uniform  Interface  •  With  HTTP  we  have  all  methods  we  need  to  manipulate  Web  resources  (CRUD  interface)  –  Create  =  POST  (or  PUT)  –  Read  =  GET  –  Update  =  PUT  –  Delete  =  DELETE  

I/#)$'& 9)5J)5&9oQ&

I/#)$'& N::3& *)+&

/234567839&:!*,&

!"1/&:!*,&

34  

Page 35: INFO/CS(4302( Web(Informaon(Systems - Cornell University · INFO/CS(4302(Web(Informaon(Systems (FT2012(Week(7:(RESTful(Webservice(APIs((H(Bernhard(Haslhofer(H(

Mapping  Web  Service  Opera6ons  to  CRUD  

•  C(reate):    – order  at  Etsy,  message  on  Facebook  wall,  ???  

•  R(read):  – ???  

•  U(pdate):  – user  account  on  Etsy,  ???  

•  D(elete):  – order  at  Etsy,  ???  

35  

Page 36: INFO/CS(4302( Web(Informaon(Systems - Cornell University · INFO/CS(4302(Web(Informaon(Systems (FT2012(Week(7:(RESTful(Webservice(APIs((H(Bernhard(Haslhofer(H(

Safe  and  Idempotent  Behavior  

•  Safe  methods  can  be  ignored  or  repeated  without  side-­‐effects:  GET  and  HEAD  

•  Idempotent  methods  can  be  repeated  without  side-­‐effects:  PUT  and  DELETE  

•  Unsafe  and  non-­‐idempotent  methods  should  be  treated  with  care:  POST  

36  

Page 37: INFO/CS(4302( Web(Informaon(Systems - Cornell University · INFO/CS(4302(Web(Informaon(Systems (FT2012(Week(7:(RESTful(Webservice(APIs((H(Bernhard(Haslhofer(H(

Uniform  Interface  

•  CREATE  a  new  resource  with  HTTP  POST  

POST /movies HTTP/1.1Host: example.com<movie ... />

201 CreatedLocation: http://example.com/movies/1234

400 Bad Request

Client Server

500 Internal Server Error

37  

Page 38: INFO/CS(4302( Web(Informaon(Systems - Cornell University · INFO/CS(4302(Web(Informaon(Systems (FT2012(Week(7:(RESTful(Webservice(APIs((H(Bernhard(Haslhofer(H(

Example  POST  Request  

POST /movies HTTP/1.1!Host: example.com!...!!<?xml...>!<movie>!

!<title>The Godfather</title>!!<synopsis>...</synopsis>!

</movie>!

38  

Page 39: INFO/CS(4302( Web(Informaon(Systems - Cornell University · INFO/CS(4302(Web(Informaon(Systems (FT2012(Week(7:(RESTful(Webservice(APIs((H(Bernhard(Haslhofer(H(

POST  Seman6cs  

•  POST  creates  a  new  resource  •  The  server  decides  on  the  resource’s  URI  •  POST  is  not  idempotent  – A  sequence  of  two  or  more  POST  requests  has  side-­‐effects  

– Human  Web:  •  “Do  you  really  want  to  post  this  form  again?”  •  “Are  you  sure  you  want  to  purchase  that  item  again?”  

–  Programma6c  Web:  •  if  you  post  twice,  you  create  two  resources  

39  

Page 40: INFO/CS(4302( Web(Informaon(Systems - Cornell University · INFO/CS(4302(Web(Informaon(Systems (FT2012(Week(7:(RESTful(Webservice(APIs((H(Bernhard(Haslhofer(H(

Uniform  Interface  

•  CREATE  a  new  resource  with  HTTP  PUT  

PUT /movies/1234 HTTP/1.1Host: example.com<movie ... />

200 OK

404 Not Found

Client Server

409 Conflict

500 Internal Server Error

40  

Page 41: INFO/CS(4302( Web(Informaon(Systems - Cornell University · INFO/CS(4302(Web(Informaon(Systems (FT2012(Week(7:(RESTful(Webservice(APIs((H(Bernhard(Haslhofer(H(

Example  PUT  Request  

PUT /movies/1234 HTTP/1.1!Host: example.com!...!!<?xml...>!<movie>!

!<title>The Godfather</title>!!<synopsis>...</synopsis>!

</movie>!

41  

Page 42: INFO/CS(4302( Web(Informaon(Systems - Cornell University · INFO/CS(4302(Web(Informaon(Systems (FT2012(Week(7:(RESTful(Webservice(APIs((H(Bernhard(Haslhofer(H(

PUT  Seman6cs  

•  PUT  creates  a  new  resource  •  The  client  decides  on  the  resource’s  URI  •  PUT  is  idempotent  – mul6ple  PUT  requests  have  no  side  effects  – but  it  changes  the  resource  state  

42  

Page 43: INFO/CS(4302( Web(Informaon(Systems - Cornell University · INFO/CS(4302(Web(Informaon(Systems (FT2012(Week(7:(RESTful(Webservice(APIs((H(Bernhard(Haslhofer(H(

Create  with  PUT  or  POST?  

•  The  generic  answer:  it  depends  J  •  Considera6ons  – PUT  if  client  

•  can  decide  on  the  URI  •  sends  complete  representa6on  to  the  server  

– POST  if  server  creates  the  URI  (algorithmically)  – some  firewalls  only  allow  GET  and  POST  – POST  is  common  prac6ce  

43  

Page 44: INFO/CS(4302( Web(Informaon(Systems - Cornell University · INFO/CS(4302(Web(Informaon(Systems (FT2012(Week(7:(RESTful(Webservice(APIs((H(Bernhard(Haslhofer(H(

CREATE  with  PUT  Example  

# Create Amazon S3 bucket!!PUT / HTTP/1.1!Host: colorpictures.s3.amazonaws.com!Content-Length: 0!Date: Wed, 01 Mar 2009 12:00:00 GMT!Authorization: AWS 15B4D3461F177624206A:xQE0diMbLRepdf3YB+FIEXAMPLE=!!# Add Object to a bucket!!PUT /my-image.jpg HTTP/1.1!Host: colorpictures.amazonaws.com!Date: Wed, 12 Oct 2009 17:50:00 GMT!

44  

Page 45: INFO/CS(4302( Web(Informaon(Systems - Cornell University · INFO/CS(4302(Web(Informaon(Systems (FT2012(Week(7:(RESTful(Webservice(APIs((H(Bernhard(Haslhofer(H(

Uniform  Interface  

•  READ  an  exis6ng  resource  with  HTTP  GET  

GET /movies/1234 HTTP/1.1Host: example.com

200 OK<movie ... />

404 Not Found

Client Server

500 Internal Server Error

45  

Page 46: INFO/CS(4302( Web(Informaon(Systems - Cornell University · INFO/CS(4302(Web(Informaon(Systems (FT2012(Week(7:(RESTful(Webservice(APIs((H(Bernhard(Haslhofer(H(

Example  GET  Request  /  Response  Request:!!GET /movies/1234 HTTP/1.1!Host: example.com!Accept: application/xml!...!!Response:!!HTTP/1.1 200 OK!Date: ...!Content-Type: application/xml!!<?xml...>!<movie>!

!<title>The Godfather</title>!!<synopsis>...</synopsis>!

</movie>!

46  

Page 47: INFO/CS(4302( Web(Informaon(Systems - Cornell University · INFO/CS(4302(Web(Informaon(Systems (FT2012(Week(7:(RESTful(Webservice(APIs((H(Bernhard(Haslhofer(H(

GET  Seman6cs  

•  GET  retrieves  the  representa6on  (  =  the  current  state)  of  a  resource  

•  GET  is  safe  (implies  idempotent)  – does  not  change  state  of  resource  – has  no  side-­‐effects  

•  If  GET  goes  wrong  – GET  it  again!  – no  problem  because  it  safe  (and  idempotent)  

47  

Page 48: INFO/CS(4302( Web(Informaon(Systems - Cornell University · INFO/CS(4302(Web(Informaon(Systems (FT2012(Week(7:(RESTful(Webservice(APIs((H(Bernhard(Haslhofer(H(

Uniform  Interface  

•  UPDATE  an  exis6ng  resource  with  HTTP  PUT  

PUT /movies/1234 HTTP/1.1Host: example.com<movie ... />

200 OK

404 Not Found

Client Server

409 Conflict

500 Internal Server Error

48  

Page 49: INFO/CS(4302( Web(Informaon(Systems - Cornell University · INFO/CS(4302(Web(Informaon(Systems (FT2012(Week(7:(RESTful(Webservice(APIs((H(Bernhard(Haslhofer(H(

When  PUT  goes  wrong  

•  If  we  get  5xx  error,  or  some  4xx  errors  –  simply  PUT  again!  –  no  problem,  because  PUT  is  idempotent  

•  If  we  get  errors  indica6ng  incompa6ble  states  then  do  some  forward/backward  compensa6on  work  and  maybe  PUT  again  –  409  Conflict  (e.g.,  change  your  username  to  a  name  that  is  already  taken)  

–  417  Expecta6on  Failed  (the  server  won’t  accept  your  representa6on  –  fix  it,  if  possible)  

49  

Page 50: INFO/CS(4302( Web(Informaon(Systems - Cornell University · INFO/CS(4302(Web(Informaon(Systems (FT2012(Week(7:(RESTful(Webservice(APIs((H(Bernhard(Haslhofer(H(

Uniform  Interface  

•  DELETE  an  exis6ng  resource  with  HTTP  DELETE  

DELETE /movies/1234 HTTP/1.1Host: example.com

200 OK

404 Not Found

Client Server

405 Method Not Allowed

500 Internal Server Error

50  

Page 51: INFO/CS(4302( Web(Informaon(Systems - Cornell University · INFO/CS(4302(Web(Informaon(Systems (FT2012(Week(7:(RESTful(Webservice(APIs((H(Bernhard(Haslhofer(H(

DELETE  Seman6cs  

•  Stop  the  resource  from  being  accessible  –  logical  delete  – not  necessarily  physical  

•  If  DELETE  goes  wrong  –  try  it  again!  – DELETE  is  idempotent    

51  

Page 52: INFO/CS(4302( Web(Informaon(Systems - Cornell University · INFO/CS(4302(Web(Informaon(Systems (FT2012(Week(7:(RESTful(Webservice(APIs((H(Bernhard(Haslhofer(H(

The  Resource-­‐Oriented  Architecture  

•  A  set  of  design  principles  for  building  RESTful  Web  Services  – Addressability  – Uniform  interface  – Connectedness  – Statelessness  

52  

Page 53: INFO/CS(4302( Web(Informaon(Systems - Cornell University · INFO/CS(4302(Web(Informaon(Systems (FT2012(Week(7:(RESTful(Webservice(APIs((H(Bernhard(Haslhofer(H(

Connectedness  •  In  RESTful  services,  resource  representa6ons  are  hypermedia  

•  Served  documents  contain  not  just  data,  but  also  links  to  other  resources  

HTTP/1.1 200 OK!Date: ...!Content-Type: application/xml!!<?xml...>!<movie>!

!<title>The Godfather</title>!!<synopsis>...</synopsis>!!<actor>http://example.com/actors/567</actor>!

</movie>!

53  

Page 54: INFO/CS(4302( Web(Informaon(Systems - Cornell University · INFO/CS(4302(Web(Informaon(Systems (FT2012(Week(7:(RESTful(Webservice(APIs((H(Bernhard(Haslhofer(H(

The  Resource-­‐Oriented  Architecture  

•  A  set  of  design  principles  for  building  RESTful  Web  Services  – Addressability  – Uniform  interface  – Connectedness  – Statelessness  

54  

Page 55: INFO/CS(4302( Web(Informaon(Systems - Cornell University · INFO/CS(4302(Web(Informaon(Systems (FT2012(Week(7:(RESTful(Webservice(APIs((H(Bernhard(Haslhofer(H(

Statelessness  

•  Statelessness  =  every  HTTP  request  executes  in  complete  isola6on  

•  The  request  contains  all  the  informa6on  necessary  for  the  server  to  fulfill  that  request  

•  The  server  never  relies  on  informa6on  from  a  previous  request  –  if  informa6on  is  important  (e.g.,  user-­‐authen6ca6on),  the  client  must  send  it  again  

55  

Page 56: INFO/CS(4302( Web(Informaon(Systems - Cornell University · INFO/CS(4302(Web(Informaon(Systems (FT2012(Week(7:(RESTful(Webservice(APIs((H(Bernhard(Haslhofer(H(

Statelessness  •  This  constraint  does  not  say  “stateless  applica6ons”!  –  for  many  RESTful  applica6ons,  state  is  essen6al  –  e.g.,  shopping  carts  

•  It  means  to  move  state  to  clients  or  resources  •  State  in  resources  –  the  same  for  every  client  working  with  the  service  –  when  a  client  changes  resource  state  other  clients  see  this  change  as  well  

•  State  in  clients  (e.g.,  cookies)  –  specific  to  client  and  has  to  be  maintained  by  each  client  –  makes  sense  for  maintaining  session  state  (login  /  logout)  

56  

Page 57: INFO/CS(4302( Web(Informaon(Systems - Cornell University · INFO/CS(4302(Web(Informaon(Systems (FT2012(Week(7:(RESTful(Webservice(APIs((H(Bernhard(Haslhofer(H(

State  in  the  Applica6on  

©  Erik  Wilde:  hMp://dret.net/netdret/docs/rest-­‐icwe2010/  57  

Page 58: INFO/CS(4302( Web(Informaon(Systems - Cornell University · INFO/CS(4302(Web(Informaon(Systems (FT2012(Week(7:(RESTful(Webservice(APIs((H(Bernhard(Haslhofer(H(

Statelessness  

©  Erik  Wilde:  hMp://dret.net/netdret/docs/rest-­‐icwe2010/  58  

Page 59: INFO/CS(4302( Web(Informaon(Systems - Cornell University · INFO/CS(4302(Web(Informaon(Systems (FT2012(Week(7:(RESTful(Webservice(APIs((H(Bernhard(Haslhofer(H(

Statelessness  

©  Erik  Wilde:  hMp://dret.net/netdret/docs/rest-­‐icwe2010/  59  

Page 60: INFO/CS(4302( Web(Informaon(Systems - Cornell University · INFO/CS(4302(Web(Informaon(Systems (FT2012(Week(7:(RESTful(Webservice(APIs((H(Bernhard(Haslhofer(H(

Tools  and  Frameworks  •  Ruby  on  Rails  -­‐  a  framework  for  building  RESTful  Web  applica6ons  

–  hMp://www.rubyonrails.org/  •  Restlet  -­‐  framework  for  mapping  REST  concepts  to  Java  classes  

–  hMp://www.restlet.org  •  Django  -­‐  framework  for  building  RESTful  Web  applica6ons  in  Python  •  JAX-­‐RC  specifica6on  (hMp://jsr311.java.net/)  provides  a  Java  API  for  

RESTful  Web  Services  over  the  HTTP  protocol.  •  RESTEasy  (hMp://www.jboss.org/resteasy/)  -­‐  JBoss  project  that  provides  

various  frameworks  for  building  RESTful  Web  Services  and  RESTful  Java  applica6ons.  Fully  cer6fied  JAX-­‐RC  implementa6on.  

60  

Page 61: INFO/CS(4302( Web(Informaon(Systems - Cornell University · INFO/CS(4302(Web(Informaon(Systems (FT2012(Week(7:(RESTful(Webservice(APIs((H(Bernhard(Haslhofer(H(

Readings  

•  Fielding,  Roy:  Architectural  Styles  and  the  Design  of  Network-­‐based  Sogware  Architectures  (Chapters  4-­‐6):  hMp://www.ics.uci.edu/~fielding/pubs/disserta6on/top.htm  

•  Tutorial  Design  Principles,  PaMerns  and  Emerging  Technologies  for  RESTful  Web  Services  (Cesare  Pautasso  and  Erik  Wilde):  hMp://dret.net/netdret/docs/rest-­‐icwe2010/  

  61