rest overview

21
REST Defined Creating a RESTful interface to your site

Upload: rap-payne

Post on 23-Jul-2015

79 views

Category:

Technology


1 download

TRANSCRIPT

REST Defined Creating a RESTful interface to your site

Remember, browsers make HTTP requests

HTTP requests use a verb to communicate their intent

� GET � POST � PUT � DELETE

� asdf

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

Which is better?

This …

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

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

… or this …

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

Why?

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

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

that reduces coupling

in a stateless environment …

an architectural style … REST is ...

HI, I'm Roy Fielding, the

inventor of REST.

for addressing resources …

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

A layered system has predictable parts

The usual method

Category

Thing

Action

ID

Properly RESTful url

Category

Thing

ID

(Use HTTP method as action)

Use the HTTP requests to specify the action

� GET � POST � PUT � DELETE

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

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

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

Give every thing an ID

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

Link things together

� Also easy to do if you're using HTML

Use standard methods

Resources with multiple representations

� HTML � XML � VCard � RSS

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

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

Further study �  Intro to REST

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