REST API Introduction

REST (Representational State Transfer) is an architecture that is based on web standards. It defines a set of constraints and protocols which need to be followed while creating web services. It follows the HTTP protocol and considers every component as a resource. These components can be accessed with the help of HTTP standard methods. REST architecture includes the REST server and client. REST server provides access to the resources while the rest clients modify the resources on the REST server. The unique address of the resource can be located with the help of URI (Uniform Resource Locator).  REST represents the resource in various representations such as JSON, XML, text.

REST is more robust than SOAP (Simple Object Access Protocol) because REST uses less bandwidth. REST is very light, scalable, and maintainable. SOAP uses just XML to represent the resource while REST uses various representations such as JSON, XML, text. Let us look at the most used HTTP methods which are used in REST.

HTTP Methods

The most used HTTP methods which are used in REST architecture are:

·        GET – It provides rest-client only read-only access to a resource on the REST server. With the GET HTTP request, you can read a resource that returns the data in the form of XML or JSON. It also returns a response code which should be 200 in case of successful data transfer. GET is a safe operation.

·        POST – It is used to create a new resource on the REST server. It basically creates subordinate resources and returns 201 in case of the successful creation of resources on the server. It also returns the Location header where the resource has been created. It is a safe and idempotent operation.

·        PUT – It is used to update an already existing resource on the REST server or create a new resource if the resource does not exist. On successful update of resource 200 is returned as HTTP status code while on successful creation of resource 201 is returned.

·        DELETE – It is used to remove a resource from the REST server. On successful deletion 200 status code is returned. DELETE is not a safe operation.

·        PATCH – It is used to modify the resource on the webserver. Unlike PUT you do not need to pass the complete resource to modify it while they just contain the changes which need to be incorporated. PATCH is neither idempotent nor safe operation.

How the REST architecture works?

Rest Client sends a request to the REST server with the help of the URL. The URL’s can be different such as GET, POST, PUT, and DELETE. When the server processes the request, it returns the response which can be in different representations such as XML, JSON, HTML, or text. Though, JSON is the most common and popular used representation in REST architecture. The HTTP methods (GET, PUT, POST, and DELETE) are CRUD operations. CRUD refers to (CREATE Read Update Delete) which respectively refer to (POST GET PUT DELETE) HTTP Keywords.

HTTP Request

It is a packet of information that is sent by the REST client to the REST server. This information is in the form of binary data.  The HTTP request consists of headers, request line, and an optional body. A Request-line consists of the HTTP method followed by the URI and then the HTTP protocol. Headers are used to pass additional information about the request to the server. The section which is above the body and below the HTTP request line is part of headers. It has various parameters such as Accept-Language, Content-Type, authorization, Accept-Encoding, and others. It tells the server about the language, content-type, encoding it is expecting from the server. When there is a requirement of creation or update of the resource on the server, the request body is required while for the GET operation request body is not required.

HTTP Response

The HTTP response is packed with binary data that is sent by the server to the REST client. The response consists of the Response line, Headers, and Body of the Response (Optional). Status Line has information related to the HTTP protocol version followed by Status Code and Message. The section below the status line and above the body is the Header section. It has additional information that is passed to the client. Some of the common headers are Content-Type, Content-Length, Response-Status Code. It tells the client that the response will be in this format. Last is the response data which is the actual data requested by the client. The most common representation of data is JSON. 

Conclusion

Thus, we got to know more about the REST API and the different terms associated with them. Let us now start learning about the ways to test Rest APIs. Before starting that, let us have a look at eclipse setup for rest assured for testing Rest API in our next section. 

Nancy Tuli

About the Author

Nancy Tuli