rest - blogs. · pdf file soap: rpc via http, uses http as tunnel not descriptive: ... a lot...

42
REST A brief introduction Juergen Brendel

Upload: vanminh

Post on 18-Mar-2018

220 views

Category:

Documents


1 download

TRANSCRIPT

RESTA brief introduction

Juergen Brendel

All contents Copyright © 2010, Mulesoft Inc. 2

What is it good for?

► Something with networks: APIs

Interactions

Distributed systems?

All contents Copyright © 2010, Mulesoft Inc. 3

Getting more popular

All contents Copyright © 2010, Mulesoft Inc. 4

Confusion

► So, like XML-RPC, RMI, CORBA?

Not really...

► Many “REST APIs” are not REST

( HTTP != REST)

All contents Copyright © 2010, Mulesoft Inc. 5

The origins of REST

All contents Copyright © 2010, Mulesoft Inc. 6

The origins of REST

Roy FieldingRoy Fielding

All contents Copyright © 2010, Mulesoft Inc. 7

The origins of REST

► 1999: RFC 2616: HTTP/1.1

► 2000: PhD thesis

Architectural Styles and theDesign of Network-basedSoftware Architectures

► Defined “REST”

REpresentationalState Transfer

Roy FieldingRoy Fielding

All contents Copyright © 2010, Mulesoft Inc. 8

REST: What is it NOT?

► Protocol

► API

► Framework

► Product

► Technique

► 1:1 replacement for RPC

All contents Copyright © 2010, Mulesoft Inc. 9

What is REST?

► “Architectural style”

► Constraints and principles

All contents Copyright © 2010, Mulesoft Inc. 10

Goals

► Work WITH the web, not against it

► Trade scalability for latency

► Intermediaries as active participants Caches

Proxies

Load balancers

Generic servers

All contents Copyright © 2010, Mulesoft Inc. 11

(interlude: the HTTP header)

You enter this:

http://www.foo.com/xyz/bar?name=Smith

All contents Copyright © 2010, Mulesoft Inc. 12

(interlude: the HTTP header)

You enter this:

http://www.foo.com/xyz/bar?name=Smith

Browser connects and sends:

GET /xyz/bar?name=Smith HTTP/1.1Host: www.foo.comUser­agent: Mozilla/5.0, ...Accept: */*...

All contents Copyright © 2010, Mulesoft Inc. 13

(interlude: the HTTP header)

You enter this:

http://www.foo.com/xyz/bar?name=Smith

Browser connects and sends:

GET /xyz/bar?name=Smith HTTP/1.1Host: www.foo.comUser­agent: Mozilla/5.0, ...Accept: */*...

“Verb”: HTTP method(what are we doing)

All contents Copyright © 2010, Mulesoft Inc. 14

(interlude: the HTTP header)

You enter this:

http://www.foo.com/xyz/bar?name=Smith

Browser connects and sends:

GET /xyz/bar?name=Smith HTTP/1.1Host: www.foo.comUser­agent: Mozilla/5.0, ...Accept: */*...

“Noun”: Path of URL(what are we operating on)

All contents Copyright © 2010, Mulesoft Inc. 15

REST principles, part 1

► Well known actions ('verbs')

► Resources ('nouns')

► HTTP?

All contents Copyright © 2010, Mulesoft Inc. 16

Names and standard methods

SOAP: RPC via HTTP, uses HTTP as tunnel

All contents Copyright © 2010, Mulesoft Inc. 17

Names and standard methods

POST /myService  HTTP/1.1Host: www.foo.com ...

<?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://www.foo.com/customer">  <m:GetCustomer>    <m:CustomerName>Smith</m:CustomerName>  </m:GetCustomer></soap:Body> </soap:Envelope>

SOAP: RPC via HTTP, uses HTTP as tunnel

All contents Copyright © 2010, Mulesoft Inc. 18

Names and standard methods

POST /myService  HTTP/1.1Host: www.foo.com ...

<?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://www.foo.com/customer">  <m:GetCustomer>    <m:CustomerName>Smith</m:CustomerName>  </m:GetCustomer></soap:Body> </soap:Envelope>

SOAP: RPC via HTTP, uses HTTP as tunnel

Not descriptive:Same for each request

Why is that a problem?

All contents Copyright © 2010, Mulesoft Inc. 19

Names and standard methods

http://foo.com/srv?method=GetCustomer&name=Smith

Better, but still only uses HTTP as tunnel

All contents Copyright © 2010, Mulesoft Inc. 20

Names and standard methods

http://foo.com/srv?method=GetCustomer&name=Smith

Better, but still only uses HTTP as tunnel

http://foo.com/srv?method=DeleteCustomer&name=Smithhttp://foo.com/srv?method=GetCustomer&name=Smith

cachecache??

All contents Copyright © 2010, Mulesoft Inc. 21

Names and standard methods

http://foo.com/srv?method=GetCustomer&name=Smith

Better, but still only uses HTTP as tunnel

http://foo.com/srv?method=MakeCustomer&                   name=Smith&                   first=John

http://foo.com/srv?method=DeleteCustomer&name=Smithhttp://foo.com/srv?method=GetCustomer&name=Smith

cachecache??reloadreload??

All contents Copyright © 2010, Mulesoft Inc. 22

Names and standard methods

GET /customer/Smith HTTP/1.1Host: foo.comAccept: application/json

RESTful (retrieving record)

http://foo.com/customer/Smith

All contents Copyright © 2010, Mulesoft Inc. 23

Names and standard methods

GET /customer/Smith HTTP/1.1Host: foo.comAccept: application/json

RESTful (retrieving record)

http://foo.com/customer/Smith

A collection

All contents Copyright © 2010, Mulesoft Inc. 24

Names and standard methods

GET /customer/Smith HTTP/1.1Host: foo.comAccept: application/json

RESTful (retrieving record)

http://foo.com/customer/Smith

ID of itemin collection

All contents Copyright © 2010, Mulesoft Inc. 25

Names and standard methods

GET /customer/Smith HTTP/1.1Host: foo.comAccept: application/json

RESTful (retrieving record)

http://foo.com/customer/Smith

{    “name” : “Smith”,

“first” : “Frank”}

All contents Copyright © 2010, Mulesoft Inc. 26

Names and standard methods

PUT /customer/Smith HTTP/1.1Host: foo.comContent­type: application/json

{    “name” : “Smith”,

“first” : “Frank”}

RESTful (updating record)

http://foo.com/customer/Smith

All contents Copyright © 2010, Mulesoft Inc. 27

Names and standard methods

DELETE /customer/Smith HTTP/1.1Host: foo.com

RESTful (deleting record)

http://foo.com/customer/Smith

All contents Copyright © 2010, Mulesoft Inc. 28

Names and standard methods

POST /customer HTTP/1.1Host: foo.comContent­type: application/json

{  “name”  : “Smith”,  “first” : “John”}

RESTful (creating record)

http://foo.com/customer/

All contents Copyright © 2010, Mulesoft Inc. 29

Names and standard methods

GET /customer HTTP/1.1Host: foo.comContent­type: application/json

RESTful (get collection)

http://foo.com/customer/

{    “Smith” : { “link” : “/customer/Smith”,                “method” : “GET” },    “Jones” : { “link” : “/customer/Jones”,                “method” : “GET” }}

All contents Copyright © 2010, Mulesoft Inc. 30

REST principles, part 2

► “Resources” not “Actions”

► Proper use of HTTP methods

All contents Copyright © 2010, Mulesoft Inc. 31

REST principles, part 2

► “Resources” not “Actions”

► Proper use of HTTP methods

► Stateless

► Representations / media types

► Links (HATEOAS)

All contents Copyright © 2010, Mulesoft Inc. 32

Representations

http://foo.com/customer/Smith

GET /customer/Smith HTTP/1.1

Accept: application/xml

<person> <name>Smith</name> <first>John</first></person>

GET /customer/Smith HTTP/1.1

Accept: application/json

{  “name”  : “Smith”,  “first” : “John”}

All contents Copyright © 2010, Mulesoft Inc. 33

Media types

► Preferred: IANA registered MIME types Think about intermediaries

► If necessary: Custom type

All contents Copyright © 2010, Mulesoft Inc. 34

HATEOAS

“Hypermedia as theEngine of Application State”

Or:

“There are links in the representation”

All contents Copyright © 2010, Mulesoft Inc. 35

HATEOAS

► Resources carry links Describe how state of resource can be changed

► Dynamic

► Example: Web pages

All contents Copyright © 2010, Mulesoft Inc. 36

Example: Collection

{“entries” : {

“Smith” : { “link” : “/customers/Smith”, “method” : “GET” },“Jones” : { “link” : “/customers/Jones”, “method” : “GET” }

},“links” : [

{ “link” : “/customers”, “rel” : “add”, “method” : “POST” },{ “link” : “/customers”, “rel” : “list”, “method” : “GET” }

]}

All contents Copyright © 2010, Mulesoft Inc. 37

Example: Order record

{    “id”       : 6227,    “customer” : { “link” : “/customer/Smith”, “method” : “GET” },    “item”     : { “link” : “/items/4552”, “method” : “GET” },    “quantity” : 4,    “amount”   : 5622.95,    “links” : [

{ “link” : “/orders/6227”,  “rel” : “edit”, “method” : “PUT” },{ “link” : “/orders/6227/status”,  “rel” : “status”, “method” : “GET” }

]}

All contents Copyright © 2010, Mulesoft Inc. 38

Example: Order record

{    “id”       : 6227,    “customer” : { “link” : “/customer/Smith”, “method” : “GET” },    “item”     : { “link” : “/items/4552”, “method” : “GET” },    “quantity” : 4,    “amount”   : 5622.95,    “links” : [

{ “link” : “/orders/6227”,  “rel” : “edit”, “method” : “PUT” },{ “link” : “/orders/6227/status”,  “rel” : “status”, “method” : “GET” }

]}

Could disappear,if order is finalized

All contents Copyright © 2010, Mulesoft Inc. 39

Benefit: Scalability

► Performance: Caches / load-balancers

Stateless

Agility

► Adoption: Bookmarkable links

Any HTTP enabled client

All contents Copyright © 2010, Mulesoft Inc. 40

Benefit: Simplicity / reuse

► Development: Open source app frameworks / tools

► Testing: Click it!

► Operation: Scheduling? cron + curl

Caching? squid, varnish

Security? ssl, HTTP-auth, apache, nginx, etc.

All contents Copyright © 2010, Mulesoft Inc. 41

REST challenges

► Focus on resource not action No 1:1 mapping to OO

► Requires rethinking A lot of investment into methods and RPC

► Scalability, not latency

► Frameworks Often low-level

All contents Copyright © 2010, Mulesoft Inc. 42

The end

juergen.brendel@mulesoft .com

@BrendelConsult