ReST-ful Resource Management

22
ReST-ful .NET Data Services Presented By: Joe Davis http://stygma.com Twitter: @stygz Facebook: http://www.facebook.com/joe.d avis

description

This presentation was given on 11/30/2009 at the North Idaho .NET User Group meeting in Sandpoint, Idaho by Joe Davis.

Transcript of ReST-ful Resource Management

Page 1: ReST-ful Resource Management

ReST-ful .NET Data Services

Presented By:

Joe Davis

http://stygma.com

Twitter: @stygz

Facebook: http://www.facebook.com/joe.davis

Page 2: ReST-ful Resource Management

HTTP Request Structure

Method URI Protocol

Header1: Value1

Header2: Value2

<empty line>

Body

Page 3: ReST-ful Resource Management

HTTP GET RequestGET http://www.sample.com/page.aspx?myname=Joe+Davis HTTP/1.1Host: www.sample.comUser-Agent: Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US)

Page 4: ReST-ful Resource Management

HTTP POST RequestPOST http://www.sample.com/page.html HTTP/1.1Host: www.sample.comUser-Agent: Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US)

myname=Joe+Davis

Page 5: ReST-ful Resource Management

HTTP ResponseHTTP/1.1 200 OKContent-Type: text/htmlContent-length: 95

<html><head>

<title>Sample HTML Document</title></head><body>

Hello, Nurse!</body></html>

Page 6: ReST-ful Resource Management

Quick Web Proxy Demo

• WebScarab

Page 7: ReST-ful Resource Management

Semantics

Structure(HTML)Presentation

(Style)

Behavior(Script)

Perform Action

Supply Context

Specify Resource

Page 8: ReST-ful Resource Management

The Web IS ReST

• URI = Uniform RESOURCE Identifier

• The HTML document is a “Representation” of a resource in its current “State”

• Resource representations can exist in any combination of any digital format (HTML, XML, JSON, RDF, RSS, ??)

Page 9: ReST-ful Resource Management

Identify the Resource

<PROTOCOL>://<HOST><PATH>?<QUERYSTRING>

http://www.sample.com/default.html

http://www.sample.com/category/widgets.html

<link rel=“alternate” type=“application/atom+xml” href=“http://www.sample.com/category/widgets.atom” >

http://www.sample.com/product/Super-Widget.html

<link rel=“alternate” type=“application/json” href=“http://www.sample.com/product/Super-Widget.json” >

Page 10: ReST-ful Resource Management

HTTP Methods

GET Retrieve resource headers and body

HEAD Retrieve resource HEADERS ONLY

POST Create resource (returns current state after creation—handy if server-generated resource ID is required)

PUT Create or Update resource (returns current state after update)

DELETE Deletes resource

Page 11: ReST-ful Resource Management

ReST GET Request/Response

GET http://www.mystore.com/product/Super-Widget.json HTTP/1.1

HOST: www.mystore.com

HTTP/1.1 200 OK

Content-Type: application/json

Content-Length: 53

{“ProductID”:123,”Name”:”Super Widget”,”Price”:49.99}

Page 12: ReST-ful Resource Management

ReST POST Request/Response

POST http://www.mystore.com/product/Ultra-Widget.json HTTP/1.1

HOST: www.mystore.com

Content-Type: application/json

Content-Length: 53

{”Name”:”Ultra Wigdet”,”Price”:59.99}

HTTP/1.1 201 CREATED

Content-Type: application/json

Content-Length: 53

{“ProductID”:456,”Name”:”Ultra Wigdet”,”Price”:59.99}

Page 13: ReST-ful Resource Management

ReST PUT Request/Response

PUT http://www.mystore.com/product/Ultra-Widget.json HTTP/1.1

HOST: www.mystore.com

Content-Type: application/json

Content-Length: 53

{“ProductID”:456,”Name”:”Ultra Widget”,”Price”:59.99}

HTTP/1.1 202 ACCEPTED

Content-Type: application/json

Content-Length: 53

{“ProductID”:456,”Name”:”Ultra Widget”,”Price”:59.99}

Page 14: ReST-ful Resource Management

HTTP Success Status Codes

200 OK

201 Created

202 Accepted

204 No Content

206 Partial Content

Page 15: ReST-ful Resource Management

HTTP Redirection Status Codes

300 Multiple Choices

301 Moved Permanently

302 Found (Temporary Redirection)

Page 16: ReST-ful Resource Management

HTTP Error Status Codes

400 Bad Request

401 Unauthorized

403 Forbidden

404 Resource not found

405 Method not allowed

408 Request timeout

409 Conflict

413 Request entity too large

415 Unsupported media type

Page 17: ReST-ful Resource Management

HTTP Server Error Status Codes

500 Internal Server Error

501 Not Implemented

503 Service Unavailable

505 HTTP version not supported

Page 18: ReST-ful Resource Management

Isn’t this SOAP?

• No.

Page 19: ReST-ful Resource Management

SOAP vs. ReSTGET http://my.sample.com/stock/MSFT.json HTTP/1.1HOST: my.sample.com

POST http://my.sample.com/stock HTTP/1.1HOST: my.sample.comContent-Type: application/soap+xmlContent-Length: 308

<?xml version="1.0"?><soap:Envelope xmlns:soap=“http://www.w3.org/2001/12/soap-

envelope” soap:encodingStyle="http://www.w3.org/2001/12/soap-encoding">

<soap:Body xmlns:m="http://my.sample.com/stock">  <m:GetStock>    <m:StockName>MSFT</m:StockName>  </m:GetStock></soap:Body>

</soap:Envelope>

Page 20: ReST-ful Resource Management

So what’s RDF?

• Resource Description Framework• Doesn’t require fore-knowledge of type• Applies common ontology• High discoverability• Great framework. Terrible metaphor.

– Subject/Predicate/Object ??– Entity/Attribute/Value !!

• .NET 4.0 supports tuples and dynamic types

Page 21: ReST-ful Resource Management

Sample RDF Request/ResponseGET http://my.sample.com/product#Super-Widget HTTP/1.1HOST: my.sample.com

HTTP/1.1 200 OKContent-Type: text/rdfContent-Length: 389

<?xml version="1.0"?> <rdf:RDF xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"

xmlns:product="http://my.sample.com/product#"> <product:Widget rdf:about="http://my.sample.com/product#Super-Widget">

<product:ProductID>123</product:ProductID> <product:Name>Super Widget</product:Name> <product:Price>49.99</product:Price> </product:Widget >

</rdf:RDF>

Page 22: ReST-ful Resource Management

More Info….• “How I Explained ReST to my Wife” – Ryan Tomayko

(http://tomayko.com/writings/rest-to-my-wife )• ReSTful .NET: Build and Consume ReSTful Web Services with .NET 3.5 - Jon

Flanders (http://www.amazon.com/RESTful-NET-Build-Consume-Services/dp/0596519206/ref=sr_1_1?ie=UTF8&s=books&qid=1259709396&sr=8-1 )

• PluralSight Screencast – Aaron Skonnard (http://channel9.msdn.com/shows/Endpoint/endpointtv-Screencast-Consuming-REST-services-with-HttpClient/)

• WebScarab Web Proxy Tool (http://www.owasp.org/index.php/Category:OWASP_WebScarab_Project )

• Fiddler Web Proxy Tool (http://fiddler2.com/fiddler2/ )• WCF ReST Starter Kit (http://aspnet.codeplex.com/Release/ProjectReleases.aspx?

ReleaseId=24644)