Top Banner
REST Defined Creating a RESTful interface to your site
21
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Rest overview

REST Defined Creating a RESTful interface to your site

Page 2: Rest overview

Remember, browsers make HTTP requests

Page 3: Rest overview

HTTP requests use a verb to communicate their intent

� GET � POST � PUT � DELETE

Page 4: Rest overview

� asdf

Alright. Got that out of the way. Now back to our regularly-scheduled lecture.

Page 5: Rest overview

Which is better?

This …

http://tic.com/Report.aspx?

t=Sales&y=2015&m=03!

… or this …

http://tic.com/Sales/2015/3!

Why?

Page 6: Rest overview

Reasons � SEO � Easy to type � Easy to remember � Hides implementation stack � URLs don’t have to change when the site changes. � Hackable

Page 7: Rest overview

URLs point to resources. � Uniform ________________ locator � They don’t point to pages � So RESTful URLs are more accurate, actually

Page 8: Rest overview

that reduces coupling

in a stateless environment …

an architectural style … REST is ...

HI, I'm Roy Fielding, the

inventor of REST.

for addressing resources …

Page 9: Rest overview

A RESTful URL is one that conforms to all of the constraints � Separation between client and server � Stateless communication � Cacheable � A layered system � A uniform interface

Page 10: Rest overview

A layered system has predictable parts

The usual method

Category

Thing

Action

ID

Properly RESTful url

Category

Thing

ID

(Use HTTP method as action)

Page 11: Rest overview

Use the HTTP requests to specify the action

� GET � POST � PUT � DELETE

Page 12: Rest overview

The URL should act as an API

� http://tic.com/Customer/List � http://tic.com/Customer/Create � http://tic.com/Customer/Details/71 � http://tic.com/Customer/Edit/71 � http://tic.com/Customer/Delete/71 � http://tic.com/Department � http://tic.com/Employee � http://tic.com/AddToCart/17

Page 13: Rest overview

Or better yet ...

� GET http://tic.com/Customer � POST http://tic.com/Customer � GET http://tic.com/Customer/71 � PUT http://tic.com/Customer/71 � DELETE http://tic.com/Customer/71 � GET http://tic.com/Department � GET http://tic.com/Employee � POST http://tic.com/AddToCart/17

Page 14: Rest overview

There are certain things you can do to make a URL RESTful � Give every “thing” an ID � Link things together � Use standard methods � Communicate statelessly � Provide multiple representations

Page 15: Rest overview

Give every thing an ID

� Easy to do if you're using a primary key and a database behind it

Page 16: Rest overview

Link things together

� Also easy to do if you're using HTML

Page 17: Rest overview

Use standard methods

Page 18: Rest overview

Resources with multiple representations

� HTML � XML � VCard � RSS

Page 19: Rest overview

Communicate statelessly � Aaaand yet again, the web makes this easy.

Page 20: Rest overview

tl;dr

� RESTful urls are easier to use, therefore better � Give every “thing” an ID � Link things together � Use standard methods � Communicate statelessly � Provide multiple representations

Page 21: Rest overview

Further study �  Intro to REST

�  http://www.infoq.com/articles/rest-introduction