DEV Community

Mohan Murali
Mohan Murali

Posted on

What is RESTful Service?

Web is a place where there are tons of computers talking to each other. As the web grew, it became clear that we needed a common and secure way to allow others to interact with our application. This need gave rise to Representational State Transfer Architecture or more commonly known as REST architecture. REST defines a set of constraints for how the web should behave.
REST defines a client-server communication model. The device making the request is called the Client and the device which is listening for the requests is called a Server The server is in charge of the resources. A resource can be anything like a HTML file, a JSON object, an image file or event a JavaScript file. The client-server communication happens with HTTP/HTTPS protocols. The actual logic for fetching the resource is abstracted behind the server and the client has no idea about what happens in the server. The resource might be fetched from the server the client requested or it can be fetched from some other server. The only thing client is concerned is that if they are getting the resource or not, and if they don't receive the resource, what is the reason for that? REST is also Stateless, which means that the server has no idea about the previous calls from the client. The server does not hold on to information's for the client. A web service that obeys the REST constraints is called a RESTful service.
Client-Server
The Client calls the server using http methods and gets a http response code and http response status along with the resource as the response. The most commonly used HTTP methods are

  • GET: This the the most widely used request method. When the client makes a GET request, it means that the client is asking for the resource mentioned in the URI. Multiple GET request for the same resource will result in same response from the server.
  • POST: The second most common request method. A POST request from the client comes with a request body that contains a structure of the resource itself. POST request is used to create the resource enclosed in the request body of a POST request. Every time we make a POST request, the server will create a new request even if the body of the request doesn't change.
  • PUT: A PUT request, just like the POST request contains a structure of the resource in its request body. A PUT request tries to find of the resource is already present and if its present then update the resource and if its not present it will create the resource. Hence PUT request will always give same response for same request.
  • DELETE: A DELETE request from the client means the client wants to delete resource.

The response codes from the server are in the following categories

Successful responses
When we get the expected response from the server. This includes the following status codes

  • 200 OK: This means that the request was success. This can be the result for any HTTP methods.
  • 201 Created: This means that the resource was created. This will be the response for either PUT or POST request.

Client error responses
When there is something wrong with the request made by the client we get one of the client error response.

  • 400 Bad Request: The server was not able to understand the request due to as the structure of the resource in request body is not same as the one server expects
  • 401 Unauthorized: The client is not recognized by the server and this is a resource only for recognized clients.
  • 403 Forbidden: The client does not have access to the resource. Unlike 401, here the server is aware of the client but the client does not have access to the resource.
  • 404 Not Found: The server cannot find the requested resource.

Server error responses
When there is something wrong with the server, then we get one of the server error response.

  • 500 Internal Server Error: The server encountered a situation it doesn't know how to handle.
  • 501 Not Implemented: The request method is not supported by the server.
  • 503 Service Unavailable: The server is not available to handle the request. This might be due to the server being down for maintenance.

Top comments (2)

Collapse
 
nikdark profile image
Mikita Yafremau

Very good !
I think that it's good that you add info that REST is Stateless, because many of the beginners don't know what it's.
Great 👍

Collapse
 
_mohanmurali profile image
Mohan Murali

Yea, I added all the information that I found confusing when I was learning. Thanks, glad you liked it.