Vous êtes sur la page 1sur 3

Programming Web Services - Lec. 3.2 RESTful!

DarkSwitch 2010

RESTful (Representation State Transfer)


http://en.wikipedia.org/wiki/Restful

Concept

REST-style architectures consists of clients and servers.

Clients initiate requests to servers.


Server process requests and return appropriate responses.

Requests and responses are built around the transfer of “representations” of “resources”.

A representation of a resource is typically a document that captures the current or intended


state of a resource.

At any particular time, a client can either transitioning between application states or “at
rest”.

The client begins sending requests when it is ready to transition to a new state.

Key goals of REST

Key goals of REST include:


• Scalability of component interactions
• Generality of interfaces
• Independent deployment of components
• Intermediary components to reduce latency, enforce security and encapsulate legacy
systems.

An important concept in REST is the existence of resources, each of which is referenced


with a global identifier (e.g., a URI in HTTP). In order to manipulate these resources,
components of the network (user agents and origin servers) communicate via a
standardized interface (e.g., HTTP) and exchange representations of these resources.

Any number of connectors can mediate the request, but each does so without “seeing
past” (stateless) its own request. Thus, an application can interact with a resource by
knowing two things:
• The identifier of the resource
• The action required
It does not need to know whether there are caches, proxies, gateways, firewalls, tunnels,
or anything else between it and the server actually holding the information.

RESTful web services


A RESTful web service is a simple web service implemented using HTTP and the
principles of REST. It is a collection of resources, with three defined aspects:
• The base URI for the web service, such as http://example.com/resources/
• The MIME type of the data supported by the web service. E.g., XML
• The set of operation supported by the web service using HTTP methods (e.g., POST,
GET, PUT or DELETE)

V 1.1 Last modification: 27/05/2010! Page 3


Programming Web Services - Lec. 3.2 RESTful! DarkSwitch 2010

Unlike SOAP-based web services, there is no “official” standard for RESTful web service.
This is because REST is an architecture, unlike SOAP, which is a protocol.

RESTful (Representation State Transfer) client: Curl


http://en.wikipedia.org/wiki/Curl_%28programming_language%29
http://switchyourcodinglife.blogspot.com/2010/05/pws-hw4-client-curl.html

Curl is a reflective object-oriented programming language for interactive web applications


whose goal is to provide a smoother transition from between formatting and programming;
providing a means to embed more complex objects in simple documents without having to
switch between programming languages or development platforms.

The command that you can use with Curl are:


1. GET commands
1.1 Get XML file (Default)
curl http://localhost:8080/Jersey/rest contacts

1.2 Get Json file


curl -HAccept:application/json http://localhost:8080/Jersey/rest/contacts

2. PUT command
curl -X PUT -HContent-type:application/xml --data "foobar" http://localhost:8080/Jersey/
rest/contacts/foo

3. DELETE command
curl -X DELETE http://localhost:8080/Jersey/rest/contacts/foo

RESTful (Representation State Transfer) by Matskin lecture 3


http://www.ict.kth.se/courses/ID2208/2010/lectures/SOAP2010.pdf

Introduction
Actions with resources can be done by a limited set of globally defined methods.

While REST, in general, it not limited to HTTP, in practice it is used in the context of HTTP.

Methods with RESTful

RESTful GET PUT POST DELETE


resource

CollectionURI, List the Create a new For example, creates a Delete the
such as http:// members of collection (if new entry in the entire
RESTexample. some not exists yet). collection. The actual collection
eu/resources collection Replace the function performed by
with their existing the POST method is
member collection determined by the
URIs other wise server and is usally
dependent on the
Request-URI

V 1.1 Last modification: 27/05/2010! Page 3


Programming Web Services - Lec. 3.2 RESTful! DarkSwitch 2010

RESTful GET PUT POST DELETE


resource

Element URI, Retrieve a Update the For example, creates a Delete the
such as http:// representati addressed new subordinate of the addressed
RESTexample. on of the element of the element. The actual element of
eu/resources/ addressed collection or function performed by the
ID82736 element create it with the POST method is collection.
the specified determined by the
ID server and is usually
dependent on the
Request-URI

Steps in RESTful Web SErvices requests

1. Come up with data that will go into the HTTP request


2. Form the data as an HTTP request and send it to the appropriate HTTP server
3. Parse the response data (the response code, headers and entity-body) into the data
structure the rest of your program needs

REST vs SOAP

When use REST:


• Lightweight
• Easy to build - no toolkits required
• The service producer and service consumer have a mutual understanding of the context
and content being passed along.
• when the bandwidth is particularly important and needs to be limited

When use SOAP


• Development tools
• If the service discovery is needed
• If the architecture must address complex nonfunctional requirements
• If the architecture needs to handle asynchronous processing and invocation

V 1.1 Last modification: 27/05/2010! Page 3