Prof. Cesare Pautasso
http://www.pautasso.info
[email protected]
@pautasso
REST defines the architectural style of the Web
Four design principles explain the success and the scalability of the HTTP protocol
How do we find something on the Internet?
ftp://user:[email protected]/root/public/videos/introduction.avi
Start your remote file transfer client, connect to ftp.usi.ch
using the ftp
protocol on port 21, login using your local account and password, go to my public folder root/public
and then change to the videos sub-folder and download a copy of the introduction.avi
file.
Internet Standard for resource naming and identification (originally from 1994, revised until 2005)
https://maps.google.com/maps?q=lugano&hl=en&ll=46.004906,8.954093& spn=0.01632,0.030148&sll=37.0625,-95.677068&sspn=73.825956,123.486328& t=m&hnear=Lugano,+Ticino,+Switzerland&z=16
http://map.com/search/Lugano/Parking
Prefer positional encoding
http://map.com/search?where=Lugano&what=Parking
Key-value encoding (useful for optional parameters)
HTTP Method | Safe | Idempotent | |
POST | Create a sub resource (Perform an action) | ? | ? |
GET | Retrieve the current state of the resource | YES | YES |
PUT | Create or update the state of a resource | NO | YES |
DELETE | Clear a resource (invalidate its URI) | NO | YES |
GET will return the current state of the resource. The result may change every time
GET
is a read-only operation.POST
is a read-write operation and may change the state of the resource and provoke side effects on the server.
Resources are created by many concurrent clients
What is the right way of creating resources and to initialize their state?
PUT /resource/{id}
201 Created
{id}
is unique?
POST /resource
301 Moved Permanently
Location: /resource/{id}
Resources may have multiple representations
Resource representations are controlled with meta-data (HTTP Headers)
Accept, Content-Type
)
If-Modified-Since
)
Accept-Encoding, Content-Encoding
)
Where to store hyperlinks?
Problem: How to discover the URIs of a potentially infinite and dynamically changing set of resources?
Solution: Resource Representations contain links to other resources
GET | PUT | POST | DELETE | |
/loan | ✓ | ✓ | ✓ | ✓ |
/balance | ✓ | ✗ | ✗ | ✗ |
/user | ✓ | ✓ | ✓ | ✗ |
/book | ✓ | ✓ | ✓ | ✓ |
/order | ✓ | ? | ✓ | ✗ |
POST
on the container creates new child resourcesPUT
/DELETE
for updating and removing child resourcesGET | PUT | POST | DELETE | |
/poll | ✓ | ✗ | ✓ | ✗ |
/poll/{id} | ✓ | ✓ | ✗ | ✓ |
/poll/{id}/vote | ✓ | ✗ | ✓ | ✗ |
/poll/{id}/vote/{id} | ✓ | ✓ | ✗ | ✓ |
POST /poll
<options>A,B,C</options>
201 Created
Location: /poll/42
GET /poll/42
200 OK
<options>A,B,C</options>
<votes href="/vote"/>
POST /poll/42/vote
<name>C. Pautasso</name>
<choice>B</choice>
201 Created
Location: /poll/42/vote/1
GET /poll/42
200 OK
<options>A,B,C</options>
<votes href="/vote">
<vote id="1">
<name>C. Pautasso</name>
<choice>B</choice>
</vote>
</votes>
PUT /poll/42/vote/1
<name>C. Pautasso</name>
<choice>C</choice>
200 OK
GET /poll/42
200 OK
<options>A,B,C</options>
<votes href="/vote">
<vote id="1">
<name>C. Pautasso</name>
<choice>C</choice>
</vote>
</votes>
DELETE /poll/42
200 OK
GET /poll/42
404 Not Found
Use a spacebar or arrow keys to navigate