Friday 31 July 2015

Understanding REST

Understanding REST
REST stands for Representational State Transfer.
It is stateless, client-server relationship, cacheable communications protocol and use HTTP protocol.

REST is an architecture style for designing networked applications.

It is useful over the complex mechanisms such as CORBA, RPC or SOAP to connect between machines because HTTP is used to make calls between machines.

World Wide Web itself, based on HTTP, can be viewed as a REST-based architecture.

REST is a lightweight alternative to mechanisms like RPC (Remote Procedure Calls) and Web Services (SOAP, WSDL etc.).

Principles of REST
Resources expose easily understood directory structure URIs.
Representations transfer JSON or XML to represent data objects and attributes.
Messages use HTTP methods explicitly (for example, GET, POST, PUT, and DELETE).
Stateless interactions store no client context on the server between requests. State dependencies limit and restrict scalability. The client holds session state.

HTTP methods
Use HTTP methods to map CRUD (create, retrieve, update, delete) operations to HTTP requests.

GET
Retrieve information.
Retrieve an address with an ID of 1:
GET /addresses/1

POST
Request that the resource at the URI do something with the provided entity.
Often POST is used to create a new entity, but it can also be used to update an entity.
Create a new address:
POST /addresses

PUT
Store an entity at a URI.
PUT can create a new entity or update an existing one.

A PUT request is idempotent. Idempotency is the main difference between the expectations of PUT versus a POST request.

Modify the address with an ID of 1:
PUT /addresses/1

DELETE
Request that a resource be removed.
Delete an address with an ID of 1:
DELETE /addresses/1

HTTP status codes
Status codes indicate the result of the HTTP request.
1XX – informational
2XX – success
3XX – redirection
4XX - client error
5XX - server error

Media types
The Accept and Content-Type HTTP headers can be used to describe the content being sent or requested within an HTTP request.
JSON response:
The client may set Accept to application/json if it is requesting a response in JSON.
XML response:
Set Content-Type to application/xml tells the client that the data being sent in the request is XML.

No comments:

Post a Comment

Related Posts Plugin for WordPress, Blogger...