Vous êtes sur la page 1sur 3

Programming Web Services - Lec. 3.2 RESTful!

DarkSwitch 2010

RESTful (Representation State Transfer)



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

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

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,

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


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/

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

RESTful (Representation State Transfer) by Matskin lecture 3


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



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

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

Programming Web Services - Lec. 3.2 RESTful! DarkSwitch 2010



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

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


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