restful web
DESCRIPTION
This is the slide I shared on the second community offline party of Horizon-Dalian. The topic is about restful web, and I started from web service and web history, telling people what the REST might be, and then gave six bindings of REST style.TRANSCRIPT
![Page 1: RESTful web](https://reader037.vdocument.in/reader037/viewer/2022100304/554f84c0b4c905435d8b4b31/html5/thumbnails/1.jpg)
RESTful Webfrom RESTful style to RESTful code work !!!@PengEdy
![Page 2: RESTful web](https://reader037.vdocument.in/reader037/viewer/2022100304/554f84c0b4c905435d8b4b31/html5/thumbnails/2.jpg)
�2
Table of Contents
• Web & Web Service
!• Details of REST Style
!• Some Examples
![Page 3: RESTful web](https://reader037.vdocument.in/reader037/viewer/2022100304/554f84c0b4c905435d8b4b31/html5/thumbnails/3.jpg)
�3
Web & Web Service
![Page 4: RESTful web](https://reader037.vdocument.in/reader037/viewer/2022100304/554f84c0b4c905435d8b4b31/html5/thumbnails/4.jpg)
�4
Web Service
• Normally: a method of communications over the WWW
!• W3C Definition: a software system
• Designed to support interoperable machine-to-machine interaction
• Over network
• An interface with a certain format: WSDL
• Other systems interact with the Web Service
![Page 5: RESTful web](https://reader037.vdocument.in/reader037/viewer/2022100304/554f84c0b4c905435d8b4b31/html5/thumbnails/5.jpg)
�5
The ways we are using web service:
• RPC: Remote Procedure Call
• Strong Coupling
• SOA: Service-oriented Architecture
• This is a good choice.
• REST: Representational State Transfer
• Another choice
![Page 6: RESTful web](https://reader037.vdocument.in/reader037/viewer/2022100304/554f84c0b4c905435d8b4b31/html5/thumbnails/6.jpg)
�6
Demo of RPC
• Server
getUser()
addUser()
removeUser()
updateUser()
getLocation()
addLocation()
removeLocation()
updateLocation()
• Client
exampleAppObject = new
ExampleApp(“example.com:1234”)
exampleAppObject.getUser()
![Page 7: RESTful web](https://reader037.vdocument.in/reader037/viewer/2022100304/554f84c0b4c905435d8b4b31/html5/thumbnails/7.jpg)
�7
Demo of REST
• Server
http://example.com/users/
http://example.com/users/{user}
http://example.com/findUserForm
http://example.com/locations
http://example.con/locations/{location}
http://example.com/findLocationForm
• Client
userResource = new
Resource(http://example.com/users/001)
userResource.get()
![Page 8: RESTful web](https://reader037.vdocument.in/reader037/viewer/2022100304/554f84c0b4c905435d8b4b31/html5/thumbnails/8.jpg)
�8
Our Web…
• Human Web
• For normal users
• Website, Web Application, etc.
• Programmable Web
• For program/programmer
• A set of API
![Page 9: RESTful web](https://reader037.vdocument.in/reader037/viewer/2022100304/554f84c0b4c905435d8b4b31/html5/thumbnails/9.jpg)
�9
Web History
• Stage 1: Static Content
• At the beginning of web
• Full of static HTML content, which is the papers made by the scholar
• Like a file sharing server supports Hypertext
!• Is that enough ? We want MORE !
![Page 10: RESTful web](https://reader037.vdocument.in/reader037/viewer/2022100304/554f84c0b4c905435d8b4b31/html5/thumbnails/10.jpg)
�10
Web History
• Stage 2: CGI Program
• People wanted to have more function on the web.
• API appeared.
• CGI is made by C/C++, Perl, and some other language, which is powerful but a little tricky.
!• But, we want it better…
![Page 11: RESTful web](https://reader037.vdocument.in/reader037/viewer/2022100304/554f84c0b4c905435d8b4b31/html5/thumbnails/11.jpg)
�11
Web History
• Stage 3: Script Languages
• CGI is not safe, so we have script languages.
• ASP, JSP, PHP, Java Applet, JavaScript, etc…
• HTML was mixed with scripts, which is much more safe and powerful.
!• Well, it was still tricky for developers with pure scripts.
• The mixture of HTML and script was horrible when code increased.
!• And, we want more !
![Page 12: RESTful web](https://reader037.vdocument.in/reader037/viewer/2022100304/554f84c0b4c905435d8b4b31/html5/thumbnails/12.jpg)
�12
Web History
• Stage 4: Thin Client
• All the content was produced on the server.
• And we had MVC !
• Back-end technology runs fast in this stage.
!• Still, we want more !
![Page 13: RESTful web](https://reader037.vdocument.in/reader037/viewer/2022100304/554f84c0b4c905435d8b4b31/html5/thumbnails/13.jpg)
�13
Web History
• Stage 5: Rich Interactive Application (RIA)
• Developers started to build single page application, which can be very useful on the desktop.
• Front-end technology started to run.
• Ajax, jQuery/jQuery UI, ExtJS, Prototype, etc…
• Flex, Sliverlight, JavaFX
• ……
!• Always, we want more !
![Page 14: RESTful web](https://reader037.vdocument.in/reader037/viewer/2022100304/554f84c0b4c905435d8b4b31/html5/thumbnails/14.jpg)
�14
Web History
• Stage 6: Mobile Application
• RIA is great, we now have something greater — mobile web.
• iOS, Android, Windows Phone, Blackberry, etc..
• HTML5 + CSS3 + JavaScript
!• We are happy with it now, while we both know that’s not enough.
![Page 15: RESTful web](https://reader037.vdocument.in/reader037/viewer/2022100304/554f84c0b4c905435d8b4b31/html5/thumbnails/15.jpg)
�15
Web NOW !
• Pure HTML is not enough.
• CGI and scripts suck.
• Developers, users, administrators want to be happy !
!• So, we have made our mind and technology improved:
• Resource Oriented
• HTTP 1.1: request method, status code, cache
• REST
![Page 16: RESTful web](https://reader037.vdocument.in/reader037/viewer/2022100304/554f84c0b4c905435d8b4b31/html5/thumbnails/16.jpg)
�16
RESTful and Resource Oriented Architecture
• REST: Representational State Transfer
• a style, not a standerd
• ROA
• What is resource: anything useful
• Resources must have URIs.
• Use HTTP verbs to implement CRUD
• Provide 2 kind of web service
• Human Web
• Programmable Web
![Page 17: RESTful web](https://reader037.vdocument.in/reader037/viewer/2022100304/554f84c0b4c905435d8b4b31/html5/thumbnails/17.jpg)
�17
HTTP Request Methods
• Verbs:
• OPTIONS
• HEAD
• GET
• POST
• PUT
• DELETE
• TRACE
• CONNECT
• PATCH
![Page 18: RESTful web](https://reader037.vdocument.in/reader037/viewer/2022100304/554f84c0b4c905435d8b4b31/html5/thumbnails/18.jpg)
�18
HTTP Request Methods
• Safe method: just get information, doesn’t change anything.
• GET, HEAD
• Idempotent methods: multiple identical requests should have the same effect as a single request.
• GET, HEAD, PUT, DELETE, OPTIONS, TRACE
!• We focus on: GET, POST, DELETE, PUT
![Page 19: RESTful web](https://reader037.vdocument.in/reader037/viewer/2022100304/554f84c0b4c905435d8b4b31/html5/thumbnails/19.jpg)
�19
Details of REST Style
![Page 20: RESTful web](https://reader037.vdocument.in/reader037/viewer/2022100304/554f84c0b4c905435d8b4b31/html5/thumbnails/20.jpg)
�20
Build REST from Everything
• There are two thoughts when we want build something:
• Nothing we have, we start from bringing in.
• Everything we have, we start from cutting down.
!• For REST, the author made some cut off on the former web style.
![Page 21: RESTful web](https://reader037.vdocument.in/reader037/viewer/2022100304/554f84c0b4c905435d8b4b31/html5/thumbnails/21.jpg)
�21
RESTful Binding
• Client-Server
• Separate the system into client and server
• Client call request, send it to server
![Page 22: RESTful web](https://reader037.vdocument.in/reader037/viewer/2022100304/554f84c0b4c905435d8b4b31/html5/thumbnails/22.jpg)
�22
RESTful Binding
• Stateless
• Request shall contain all the information it needs.
• Server identify requests by the info it stored in, not some info on the server.
![Page 23: RESTful web](https://reader037.vdocument.in/reader037/viewer/2022100304/554f84c0b4c905435d8b4b31/html5/thumbnails/23.jpg)
�23
RESTful Binding
• Cacheable
• Data in the request should marked as either cacheable or not, in a sensitive or insensitive way, so that client can cache the data.
![Page 24: RESTful web](https://reader037.vdocument.in/reader037/viewer/2022100304/554f84c0b4c905435d8b4b31/html5/thumbnails/24.jpg)
�24
RESTful Binding
• Uniform Interface
• Optimize the structure
• Light coupling
![Page 25: RESTful web](https://reader037.vdocument.in/reader037/viewer/2022100304/554f84c0b4c905435d8b4b31/html5/thumbnails/25.jpg)
�25
RESTful Binding
• Layered System
• Simplify the system
• Make high performance possible
![Page 26: RESTful web](https://reader037.vdocument.in/reader037/viewer/2022100304/554f84c0b4c905435d8b4b31/html5/thumbnails/26.jpg)
�26
RESTful Binding
• Code on Demand
• Expand system
![Page 27: RESTful web](https://reader037.vdocument.in/reader037/viewer/2022100304/554f84c0b4c905435d8b4b31/html5/thumbnails/27.jpg)
�27
REST Quick Tips
• Use HTTP Verbs to mean something: GET, PUT, DELETE, POST
• Provide sensible resource names
• Use HTTP response code to indicate status
• Offer JSON and XML
![Page 28: RESTful web](https://reader037.vdocument.in/reader037/viewer/2022100304/554f84c0b4c905435d8b4b31/html5/thumbnails/28.jpg)
�28
HTTP Methods
• We will only discuss the most useful four methods here:
• GET
• PUT
• POST
• DELETE
![Page 29: RESTful web](https://reader037.vdocument.in/reader037/viewer/2022100304/554f84c0b4c905435d8b4b31/html5/thumbnails/29.jpg)
�29
HTTP Methods: GET
# Add bookmark
GET /bookmarks/add_bookmark?href=http%3A%2F%2F
www.example.org%2F2009%2F10%2F10%2Fontes.html HTTP/1.1
Host: www.example.org
# Add cart
GET /add_cart?pid=1234 HTTP/1.1
Host: www.example.org
# Delete note
GET /notes/delete?id=1234 HTTP/1.1
Host: www.example.org
![Page 30: RESTful web](https://reader037.vdocument.in/reader037/viewer/2022100304/554f84c0b4c905435d8b4b31/html5/thumbnails/30.jpg)
�30
HTTP Methods: PUT
• PUT will only be used when client can decide the URI or resource.
!# Request
PUT /user/smith/address/home_address HTTP/1.1
Host: www.example.com
Content-type: application/xml;charset=UTF-8
<address>
<street>1, Main Street</street>
<city>New York</city>
</address>
![Page 31: RESTful web](https://reader037.vdocument.in/reader037/viewer/2022100304/554f84c0b4c905435d8b4b31/html5/thumbnails/31.jpg)
�31
HTTP Methods: PUT
# Response
HTTP/1.1 201 Created
Location: http://www.example.org/user/smith/address/home_address
Content-Location: http://www.example.org/user/smith/address/home_address
Content-Type: application/xml;charset=UTF-8
<address>
<id>urn:example:user:smith:address:1</id>
<atom>link rel=“self” href=“http://www.example.org/user/smith/address/home_address”/>
<street>1, Main Street</street>
<city>New York</city>
</address>
![Page 32: RESTful web](https://reader037.vdocument.in/reader037/viewer/2022100304/554f84c0b4c905435d8b4b31/html5/thumbnails/32.jpg)
�32
HTTP Methods: POST
• Create new resource if we don’t know the URI
• Update resource(s) using controller
• Run research which has too many parameters
• Other actions which are not safe
# Request
POST /user/smith HTTP/1.1
Host: www.example.com
Content-type: application/xml;charset=UTF-8
Slug: Home Address
<address>
<street>1, Main Street</street>
<city>New York</city>
</address>
![Page 33: RESTful web](https://reader037.vdocument.in/reader037/viewer/2022100304/554f84c0b4c905435d8b4b31/html5/thumbnails/33.jpg)
�33
HTTP Methods: POST
# Response
HTTP/1.1 201 Created
Location: http://www.example.org/user/smith/address/home_address
Content-Location: http://www.example.org/user/smith/address/home_address
Content-Type: application/xml;charset=UTF-8
<address>
<id>urn:example:user:smith:address:1</id>
<atom>link rel=“self” href=“http://www.example.org/user/smith/address/home_address”/>
<street>1, Main Street</street>
<city>New York</city>
</address>
![Page 34: RESTful web](https://reader037.vdocument.in/reader037/viewer/2022100304/554f84c0b4c905435d8b4b31/html5/thumbnails/34.jpg)
�34
HTTP Methods: DELETE
• DELETE a resource on the web.
!# Request
DELETE /users/john HTTP/1.1
Host: www.example.com
![Page 35: RESTful web](https://reader037.vdocument.in/reader037/viewer/2022100304/554f84c0b4c905435d8b4b31/html5/thumbnails/35.jpg)
�35
Resource Naming
• To name a resource to make it readable and programmable
• URI: the name of resource as well as an address on the web
• A RESTful URI should refer to a resource that is a thing, not an action.
• Resource:
• Users of the system
• Courses in which a student is enrolled
• A user’s timeline of posts
• The users that follow another user (some kind of relationship)
• An article or a news on the web
• etc…
![Page 36: RESTful web](https://reader037.vdocument.in/reader037/viewer/2022100304/554f84c0b4c905435d8b4b31/html5/thumbnails/36.jpg)
�36
Resource Naming
• Some examples:
• To insert/create a new customer in the system:
• POST http://example.com/customers
• To read a customer with Customer ID #12345
• GET http://example.com/customers/12345
• the same URI would be used for PUT & DELETE, to update and delete
• For reading , updating, deleting, product #12345:
• GET/PUT/DELETE http://example.com/pruducts/12345
![Page 37: RESTful web](https://reader037.vdocument.in/reader037/viewer/2022100304/554f84c0b4c905435d8b4b31/html5/thumbnails/37.jpg)
�37
Resource Naming
• More examples:
• To create an order for a customer:
• POST http://example.com/customers/12345/orders
• GET http://example.com/customers/12345/orders
• What will it return ?
• POST http://example.com/customers/12345/orders/2334/lineitems
• What will this return ?
![Page 38: RESTful web](https://reader037.vdocument.in/reader037/viewer/2022100304/554f84c0b4c905435d8b4b31/html5/thumbnails/38.jpg)
�38
Resource Naming
• More and more real examples:
• Twitter: https://dev.twitter.com/docs/api
• Facebook: http://develpoers.facebook.com/docs/api/
• LinkedIn: https://developer.linkedin.com/apis
• more…
![Page 39: RESTful web](https://reader037.vdocument.in/reader037/viewer/2022100304/554f84c0b4c905435d8b4b31/html5/thumbnails/39.jpg)
�39
RESTful API Demo
![Page 40: RESTful web](https://reader037.vdocument.in/reader037/viewer/2022100304/554f84c0b4c905435d8b4b31/html5/thumbnails/40.jpg)
�40
Build a website
![Page 41: RESTful web](https://reader037.vdocument.in/reader037/viewer/2022100304/554f84c0b4c905435d8b4b31/html5/thumbnails/41.jpg)
�41
Build a set of API
![Page 42: RESTful web](https://reader037.vdocument.in/reader037/viewer/2022100304/554f84c0b4c905435d8b4b31/html5/thumbnails/42.jpg)
�42
Programming Languages and Frameworks
• Java
• PHP
• Python
• Ruby
• JavaScript (Node.js)
• C#
• etc…
![Page 43: RESTful web](https://reader037.vdocument.in/reader037/viewer/2022100304/554f84c0b4c905435d8b4b31/html5/thumbnails/43.jpg)
�43
Thanks for your listening !