RESTful Web Services Interview Questions

Q:- What is a Web Service?

Web service is a way to communicate between two applications over the network.

Q:- What are the advantages of Web Services?

Some of the advantages of web services are:

  1. Reusability: One web service can be used by many client applications at the same time.
  2. Loosely Coupled: In Web services, clients code is totally independent with server code
  3. Versioning: Multiple service versions can be running at same time.
  4. Interoperability: Web services are accessible over the network and runs on HTTP/SOAP protocol and uses XML/JSON to transport data, hence it can be developed in any programming language.
  5. Easy Deployment: Easy to deploy and integrate, just like web applications.
Q:- What are different types of Web Services?

There are two types of web services:

  1. SOAP Web Services
  2. Restful Web Services
Q:- Difference between SOAP vs REST Web Services?
Q:- What is REST?

REST stands for Representational State Transfer.

REST is an architectural style that defines a set of constraints to be used for creating RESTful web services

Q:- What is RESTful Web Services?

Web services which follows 6 REST Architectural Constraints are called RESTful web services

Q:- What are REST Architectural Constraints?

Following are the 6 REST Architectural Constraints:

  1. Uniform Interface
  2. Client Server Architecture
  3. Stateless
  4. Cacheable
  5. Layered System
  6. Code on demand (optional)
    1. Uniform Interface:

      The uniform interface constraint defines the interface between clients and servers.
      The uniform interface constraint is fundamental to the design of any RESTful web services. It simplifies and decouples the architecture, which enables each part to evolve independently

There are four principles of the uniform interface:

      • Identifying the resource:

        Each resource must have a specific URI to be made available.

        • code
        • source
        1. //Get user detail by user_id
        2. GET https://www.fullstacktutorials.com/user/1
      • Resource representation:

        This shows how the resource will return to the client, resource representation can be in HTML, XML, JSON, TXT etc.

        • code
        • source
        1. //JSON format
        2. {
        3. “id”: “1”,
        4. “name”: “Full Stack Tutorials”,
        5. “age”: “25”,
        6. “status”: “active”
        7. }
      • Self-descriptive Messages:

        Each message includes enough information to describe how to process the message.

      • Hypermedia as the Engine of Application State (HATEOAS):
        • Client deliver state via body contents, query-string parameters, request headers and the requested URI (the resource name).
        • Server deliver state to clients via body content, response codes, and response headers.

        This is technically referred-to as hypermedia (or hyperlinks within hypertext)

    1. Client Server Architecture:
      • REST application should have a client-server architecture.
      • The client and server should act independently. The separation between the client and server improves portability of the client and scalability of the server.
    2. Stateless:

      No client session state on the server.

      Stateless means that the server doesn’t store any state about the client session on the server side.

      • Statelessness means that every HTTP request happens in complete isolation. When the client makes an HTTP request, it includes all the information necessary for the server to fulfill that request. The server never relies on information from previous requests. If that information was important, the client would have to send it again in subsequent request.
      • It’s easier to distribute a stateless application across load-balanced servers.
      • A stateless application is also easy to cache
      • There are actually two kinds of state. Application State that lives on the client and Resource State that lives on the server.
      • A web service only needs to care about your application state when you’re actually making a request
  1. Cacheable:
    • Since many times APIs requesting the same resources, So it is necessary that these responses might be cached, avoiding unnecessary processing will significantly increase performance.
    • Caching partially or completely eliminates some client-server interactions, which further improves scalability and performance.
    • Caching can be implemented on the server or client side.
  2. Layered System:
    • REST allows you to use a layered system architecture.
    • A layered system architecture means that there may be many things in between the client and the server.
    • Intermediaries servers such as proxy servers, load balancers, cache servers, gateways etc can be inserted between client and server to improve performance, security etc.

    #Example: In layered system architecture you deploy the APIs on server A, and store data on server B and authenticate requests in Server C. So, A client cannot ordinarily tell whether it is connected directly to the end server, or to an intermediary along the way.

  3. Code on demand (optional):
    • It is optional.
    • An API/Web Services can be RESTful even without providing code on demand.
    • REST allows client functionality to be extended by downloading and executing code in the form of applets or scripts etc.

    #Example: A clients may call an API to get any UI widget rendering code etc.

By Adopting all above 6 REST Architectural Constraints you can build a truly RESTful Web Services. If for some reasons you may skipped one or two constraints. Do not worry, you APIs or Web Services are still a RESTful Web Services – but not “truly RESTful”.

You may also like – MongoDB Interview Questions Answers

Q:- What are the HTTP Methods (HTTP Verbs) supported by REST?

Following are the most popular HTTP Methods:

    1. GET – GET method is used to **read** (or retrieve) a representation of a resource. GET requests must be safe and idempotent.
    2. POST – POST method is mostly used to **create** new resources. it can also be used to update an resource.
    3. PUT – PUT is mostly used for **update** resources
    4. PATCH – PATCH is used for partial **modify** a resource.
    5. DELETE – DELETE is used to **delete** a resource identified by a URI.
  1. HEAD – It returns meta information about the request URL.
Summary of HTTP Methods for RESTful APIs
HTTP Methods Idempotent Safe Method
GET yes yes
HEAD yes yes
PUT yes No
DELETE yes No
POST No No
Q:- What are differences between PUT and POST methods?

PUT – PUT is used for updating resource. It is idempotent.

POST – POST is used for creating a new resource. It is not idempotent.

Idempotent means result of multiple successful request will not change state of resource after initial application

OR

An idempotent method means that the result of a successful performed request is independent of the number of times it is executed.

Q: – What are differences between PUT and PATCH?

Difference is that when you want to update a resource with PUT method, you have to send the complete payload as the request whereas with PATCH, you only send the parameters which you want to update.

PUT (Complete replacement/update of a resource)- It is mandatory to send complete payload.

PATCH (Partial Update)- It requires only fields which we are going to update.

Example:

Suppose we have a resource that holds the first name and last name of a person.

{ “first_name”: “FullStack”, “last_name”: “Tutorials”, “age”: “23”, “status”: “active” }

If we want to change the first name then we send a PUT request for Update

Here, although we are only changing the first name, with PUT request we have to send the complete payload of that entity. In other words, it is mandatory to send all values again (complete payload)

When we send a PATCH request, however, we only send the data which we want to update. In other words, we only send the first name to update, no need to send the complete payload. this is the reason, PATCH request requires less bandwidth.

Q:- What is Resource in REST?

REST architecture treats every content as a resource.

Resources can be anything e.g. text files, HTML Pages, Images, Videos or any Dynamic Data.

Q:- Which protocol used by RESTful Web Services?

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

Q:- What is the most popular way to represent a Resource in RESTful Web-Services?

In REST Architecture, Resource can be represented in following ways:

  1. JSON
  2. XML
  3. HTML
  4. TEXT
Q:- What is the Core Components of an HTTP Request?

Each HTTP request includes five key elements.

    1. HTTP Verbs which indicates HTTP methods such as GET, PUT, POST, and DELETE.
    2. URI stands for Uniform Resource Identifier (URI). It is the identifier for the resource on the server.
    3. HTTP Version which indicates HTTP version, for example-HTTP v1.1.
    4. Request Header carries metadata (as key-value pairs) for the HTTP Request message. Metadata could be a client (or browser) type, the format that client supports, message body format, and cache settings.
  1. Request Body indicates the message content or resource representation.
Q:- What is the Core Components of an HTTP Response?

Every HTTP response includes four key elements.

  1. HTTP Version – Indicates HTTP version, for example-HTTP v1.1.
  2. Response Header – Contains metadata for the HTTP response message stored in the form of key-value pairs. For example, content length, content type, response date, and server type.
  3. Response Body – Indicates response message content or resource representation.
  4. Status/Response Code – Indicates Server status for the resource present in the HTTP request.For example, 404 means resource not found and 200 means response is ok.
Q:- What is Messaging in RESTful Web Services?

RESTful web services make use of HTTP protocol as a medium of communication between client and server. The client sends a message in the form of an HTTP Request. In response, the server transmits the HTTP Response. This technique is called Messaging. These messages contain message data and metadata i.e. information about the message itself.

Q:- What is the use of Options Method in RESTful Web Services?

This method lists down all the operations a web service supports. It makes read-only requests to the server.

Q:- What is URI? Explain It’s purpose in RESTful based Web Services?

URI stands for Uniform Resource Identifier. In REST architecture, URI is the identifier for the Resource.

The purpose of a URI is to locate a resource(s) on the server hosting the web service

Q:- What types of tools are available to test RESTful Web Services?

Following are some most popular and most frequently used tools that can help in testing the SOAP and RESTful web services.

  1. SOAP UI tool.
  2. Postman extension for Chrome.
  3. Poster for Firefox browser.
Q:- Explain those factors that help us to decide about the style of Web Service to use – SOAP OR REST?

Generally, REST web service is preferred due to its simplicity, performance, scalability, and support for multiple data formats.

Whereas, SOAP is preferred where services require an advanced level of security and transactional reliability.

General facts – before opting for any of the styles – SOAP OR REST.
  1. Does the service expose data or business logic? To expose data REST will be a better choice and SOAP for logic.
  2. If the consumer or the service providers require a formal contract, then SOAP can provide such a contract via WSDL.
  3. Need to support multiple data formats. REST supports this.
  4. Support for AJAX calls. REST can use the XMLHttpRequest.
  5. Synchronous and asynchronous calls – SOAP enables both synchronous/asynchronous operations whereas REST has built-in support for synchronous.
  6. Stateless or Stateful calls -REST is suited for stateless operations.
Following are some of the advanced-level facts that you should consider as well.
  1. Security requirement – SOAP provides a high level of security.
  2. Transaction support – SOAP has good support for transaction management.
  3. Limited bandwidth – SOAP has a lot of overhead when sending/receiving packets since it’s XML based, requires a SOAP header. However, REST requires less bandwidth to send requests to the server. Its messages are mostly built using JSON.
  4. Ease of use – It is easy to implement, test, and maintain REST based application.

You may also like – JavaScript Interview Questions Answers

Q:- What are Microservices?

“Microservices is a phenomenon of building applications as a collection of multiple independent services which are loosely coupled”.

Microservices is an Architectural Patterns which decompose the application into various modules which are independent of each other.

Advantages of Microservices:
  1. Reusability – Once a module developed can be used multiple times.
  2. Maintainability – Easy to maintain.
  3. Technology Independent – There is no need of working with single technology for the entire application as like of monolithic kernel.Various technologies can be used to build the application and it makes developers to comfortably work as their interest.
  4. Deployment – The primary advantage of Microservices is that the modules can be independently deployed.
Q:- What is the difference between URI, URL and URN?
1. Uniform Resource Identifier (URI):
  • Uniform Resource Identifier (URI) is a string of characters used to identify a resource on the Internet
  • An URI identifies a resource either by location, or a name, or both. A URI has two specializations known as URL and URN
2. Uniform Resource Locator (URL):
  • An Uniform Resource Locator (URL) is a subset of the Uniform Resource Identifier (URI).
  • An Uniform Resource Locator (URL) contains information about how to fetch a resource from its location.
  • An URL may be http, https, ftp etc.

#Example:

  • https://www.fullstacktutorials.com/interviews/restful-web-services-interview-questions-26.html
  • ftp://fullstacktutorials.com/restfulapis.zip
3. Uniform Resource Name (URN):
  • An Uniform Resource Name (URN) is a subset of the Uniform Resource Identifier (URI).
  • URN Identifies a resource by name
  • It always starts with the prefix urn:

#Example:

  • urn:isbn:0123456759 to identify a book by its ISBN number
Q:- What is Swagger?

Swagger is a set of open-source tools built around the OpenAPI Specification that can help you design, build, document and consume REST APIs.

  • Swagger Editor – browser-based editor where you can write OpenAPI specs.
  • Swagger UI – renders OpenAPI specs as interactive API documentation.
  • Swagger Codegen – generates server stubs and client libraries from an OpenAPI spec.

OpenAPI Specification (formerly Swagger Specification) is an API description format for REST APIs.

Q:- What is the most frequenlty used HTTP Status Codes?

Following are the list of some most frequenlty used HTTP status codes

Code Message Details
200 Success Success with response body.
201 Created Success with response body.
204 Success Success with no response body.
301 Moved Permanently The requested page has been moved to a new URL.
302 Found The requested page has moved temporarily to a new URL.
400 Bad Request The server did not understand the request.
401 Unauthorized The user is not authorized to use the API.
403 Forbidden The requested operation is not permitted for the user. This error can also be caused by ACL failures, or business rule or data policy constraints.
404 Not found The requested resource was not found.
405 Method not allowed The HTTP action is not allowed for the requested REST API.
500 Internal Server Error The request was not completed because the server met an unexpected condition.

About Author

Leave a Reply

Your email address will not be published. Required fields are marked *

PAGE TOP
error

Enjoy this blog? Please spread the word :)

RSS
Follow by Email