REST API Interview Questions and Answers

Hello Friends,

Its been trend now to develop REST APIs using json and consume using front end technologies like AngularJs. Also It can be consumed in mobile apps and there are several advantages of it.

Here I am covering frequently asked REST API interview questions. Let's start now...

1) Difference between REST and SOAP.

Ans - REST is lightweight and much faster than SOAP.

SOAP has WSDL (Web Services Description Language) while REST has WADL (Web Application Description Language)

Javascript can call SOAP but its difficult to implement while with REST its easy.

SOPA invokes services by calling RPC method while REST calls services by URL path.

2) Benefits of JSON over XML

Ans - 

  • JSON parsing is generally faster than XML parsing.
  • JSON is easier to work with in some languages (such as javascript, python, and php)
  • Formatted JSON is generally easier to read than formatted XML.
  • JSON can contain Integer, String, List, Arrays. XML is just nodes and elements that needs to be parsed into Integer, String and so on before it is used by your application.

3) What are idempotent methods ?

Ans - 
They are HTTP methods which can be called multiple times and they will produce the same result. 

They are considered the safe option to update a resource on the Server.

Some examples of idempotent HTTP methods are GET, PUT, and PATCH. No matter how many times you call them, they will produce the same result with same URI.

They are safe for updating resource on Server.

For example, suppose a client wants to update a resource through POST. Since POST is not an idempotent method, calling it multiple times may result in incorrect updates.

a = 4; (is idempotent)
a++; (is not idempotent)

4) What are safe methods ?

Ans - These are HTTP methods which don't change the resource on the server side. For example using a GET or a HEAD request on a resource URL should NEVER change the resource.

5) Explain the term ‘Statelessness’ with respect to RESTful WEB service.

Ans - In REST, ST itself defines State Transfer and Statelessness means complete isolation. This means, the state of the client’s application is never stored on the server and is passed on. In this process, the clients send all the information that is required for the server to fulfill the HTTP request that has been sent. Thus every client request and the response is independent of the other with complete assurance of providing required information.

Every client passes a ‘session identifier’ which also acts as an identifier for each session.

6) Enlist advantages and disadvantages of ‘Statelessness’.

Ans - 
Advantages :

Every method required for communication is identified as an independent method i.e. there are no dependencies to other methods.
Any previous communication with the client and server is not maintained and thus the whole process is very much simplified.
If any information or metadata used earlier in required in another method, then the client sends again that information with HTTP request.
HTTP protocol and REST web service, both shares the feature of statelessness.

Disadvantages :

In every HTTP request from the client, the availability of some information regarding the client state is required by the web service.

7) What is a ‘Resource’?

Ans - Resource is defined as an object of a type which can be an image, HTML file, text data, and any type of dynamic data.

8) Different HTTP status codes

Ans - 
200 : This indicates success.
201 : This indicates resource has been successfully created.
204 : This indicates that there is no content in the response body.
404 : This indicates that there is no method available.
405 : The method specified in the request is not allowed for the resource identified by the request URI

9) Which protocol is used by RESTful Web services ?

Ans - RESTful web services use the HTTP protocol as a medium of communication between the client and the server.

10) What is OPTIONS method ?

Ans - Identifying which HTTP methods a resource supports, e.g. can we DELETE it or update it via a PUT?

11) What is the HTTP status return code for a successful DELETE statement ?

Ans - There is no strict rule with respect to what status code your REST API should return after a successful DELETE i.e it can return 200 Ok or 204 No Content. In general, if the DELETE operation is successful and the response body is empty return 204. If the DELETE request is successful and the response body is NOT empty, return 200