Be
RESTful !
by @xavierhanin
http://slid.es/xavierhanin/be-restful
/me
Lead Architect / Developer
Bordeaux Agency Manager
@ 4SH
Community
Open Source
Web Development
REST
Elements
Constraints
RESTful Web Services
Resource
Any information that can be named can be a resource - Roy Fielding
Resource Identifier
http://restx.io/modules
http://en.wikipedia.org/wiki/Main_Page
Representation
REST components perform actions on a resource by using a representation to capture the current or intended state of that resource and transferring that representation between components - Roy Fielding
Representation
{
"name":"Acme Inc",
"address": {
"body":"111, av du truc",
"zipCode":"33700",
"city":"Merignac"
},
"_id":"51eba5766bc8e48ffeaacc89"
}
XML
<company name="Acme Inc" _id="51eba5766bc8e48ffeaacc89">
<address>
<body>111, av du truc</body>
<zipcode>33700</zipcode>
<city>Merignac</city>
</address>
</company>
Uniform interface
HTTP Methods
GET
GET /companies/51ebab006bc8e48ffeaacc93
HTTP/1.1 200 OK Date: Mon, 01 Dec 2008 01:15:39 GMT Content-Type:application/json; charset=UTF-8 {
"name":"ACME Inc", "address":{ "body":"1122, pooder st",
"zipCode":"12345 CA",
"city":"Palo Alto"}, "_id":"51ebab006bc8e48ffeaacc93" }
HEAD
HEAD /companies/51ebab006bc8e48ffeaacc93
HTTP/1.1 200 OK
Date: Mon, 01 Dec 2008 01:15:39 GMT
Content-Type:application/json; charset=UTF-8
OPTIONS
OPTIONS /companies/51ebab006bc8e48ffeaacc93
Origin: http://foo.example
Access-Control-Request-Method: PUT
HTTP/1.1 200 OK
Date: Mon, 01 Dec 2008 01:15:39 GMT
Access-Control-Allow-Origin: http://foo.example
Access-Control-Allow-Methods: PUT, POST, GET, OPTIONS
PUT
PUT /companies/51ebab006bc8e48ffeaacc93
{ "name":"ACME Inc",
"address":{
"body":"1122, pooder st",
"zipCode":"12345 CA",
"city":"Palo Alto"},
"_id":"51ebab006bc8e48ffeaacc93"}
HTTP/1.1 200 OK
{ "name":"ACME Inc",
"address":{
"body":"1122, pooder st",
"zipCode":"12345 CA",
"city":"Palo Alto"},
"_id":"51ebab006bc8e48ffeaacc93"}
DELETE
DELETE /companies/51ebab006bc8e48ffeaacc93
HTTP/1.1 204 No Content
POST
POST /companies
{ "name":"ACME Inc",
"address":{
"body":"1122, pooder st",
"zipCode":"12345 CA",
"city":"Palo Alto"} }
HTTP/1.1 201 Created
{ "name":"ACME Inc",
"address":{
"body":"1122, pooder st",
"zipCode":"12345 CA",
"city":"Palo Alto"},
"_id":"51ebab006bc8e48ffeaacc93"}
Uniform Interface

Stateless
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. Session state is therefore kept entirely on the client. - Roy Fielding
Resource Design
Resource Design
Procedure
-
Figure out the data set
-
Split the data set into resources
For each kind of resource:
-
Name the resources with URIs
-
Expose a subset of the uniform interface
5. Design the representation(s) accepted from the client
6. Design the representation(s) served to the client
7. Integrate this resource into existing resources, using hypermedia links and forms
8. Consider the typical course of events: what’s supposed to happen?
9. Consider error conditions: what might go wrong?
Figure out the dataset
Split into resources
Predefined one-off resources for especially important aspects of the application.
A resource for every object exposed through the service.
Resources representing the results of algorithms applied to the data set.
Name the resources
-
Use path variables to encode hierarchy: /parent/child
-
Put punctuation characters in path variables to avoid implying hierarchy where
none exists: /parent/child1;child2
-
Use query variables to imply inputs into an algorithm, for exam- ple: /search?q=jellyfish&start=20
Going further
RESTX
Thank you !
Links
Be RESTful!
By xavierhanin
Be RESTful!
Be RESTful!
- 1,537