Introduction to RESTful Web Services

REST stands for REpresentational State Transfer. It is developed by Roy Thomas Fielding, who also developed HTTP. The main goal of RESTful web services is to make web services more effective. RESTful web services try to define services using the different concepts that are already present in HTTP. REST is an architectural approach, not a protocol.

It does not define the standard message exchange format. We can build REST services with both XML and JSON. JSON is more popular format with REST. The key abstraction is a resource in REST. A resource can be anything. It can be accessed through a Uniform Resource Identifier (URI). For example:

The resource has representations like XML, HTML, and JSON. The current state capture by representational resource. When we request a resource, we provide the representation of the resource. The important methods of HTTP are:

  • GET: It reads a resource.
  • PUT: It updates an existing resource.
  • POST: It creates a new resource.
  • DELETE: It deletes the resource.

For example, if we want to perform the following actions in the social media application, we get the corresponding results.

POST /users: It creates a user.

GET /users/{id}: It retrieves the detail of a user.

GET /users: It retrieves the detail of all users.

DELETE /users: It deletes all users.

DELETE /users/{id}: It deletes a user.

GET /users/{id}/posts/post_id: It retrieve the detail of a specific post.

POST / users/{id}/ posts: It creates a post of the user.

Further, we will implement these URI in our project.

HTTP also defines the following standard status code:

  • 404: RESOURCE NOT FOUND
  • 200: SUCCESS
  • 201: CREATED
  • 401: UNAUTHORIZED
  • 500: SERVER ERROR

RESTful Service Constraints

  • There must be a service producer and service consumer.
  • The service is stateless.
  • The service result must be cacheable.
  • The interface is uniform and exposing resources.
  • The service should assume a layered architecture.

Advantages of RESTful web services

  • RESTful web services are platform-independent.
  • It can be written in any programming language and can be executed on any platform.
  • It provides different data format like JSON, text, HTML, and XML.
  • It is fast in comparison to SOAP because there is no strict specification like SOAP.
  • These are reusable.
  • They are language neutral.





Latest Courses