What is a REST API? (RESTful Web Services)
A REST API (Representational State Transfer) is an architectural style used for building web services that communicate over HTTP.
A RESTful Web Service follows REST principles by exposing resources (such as Users, Orders, Products, or Employees) through URIs (Uniform Resource Identifiers) and allowing clients to perform operations using standard HTTP methods like GET, POST, PUT, DELETE, and PATCH.
REST is one of the most widely used architectures for developing modern web and mobile applications because it is lightweight, scalable, and easy to consume.
Interview Answer
"A REST API is an architectural style for building web services that communicate over HTTP. It exposes resources through URIs and uses standard HTTP methods like GET, POST, PUT, DELETE, and PATCH to perform CRUD operations. REST APIs are stateless, lightweight, and usually exchange data in JSON format."
Characteristics of a REST API
A REST API follows several core architectural principles.
| Characteristic | Description |
|---|---|
| Stateless | Every request contains all the required information. The server does not maintain client session data. |
| Client-Server Architecture | Client and server are independent and communicate using HTTP. |
| Cacheable | Responses can be cached to improve performance and reduce server load. |
| Uniform Interface | APIs use consistent resource naming and standard HTTP methods. |
| Layered System | Additional layers such as security, caching, or load balancing can exist between client and server. |
| Uses HTTP Methods | Uses standard methods like GET, POST, PUT, DELETE, and PATCH. |
| Resource-Based | Everything is treated as a resource and identified using a URI. |
| Supports Multiple Data Formats | Supports JSON, XML, HTML, and plain text. JSON is the most commonly used format. |
REST API Architecture
Client (Web/Mobile App)
│
HTTP Request
│
▼
REST API / Web Service
│
Business Logic Layer
│
▼
Database
│
HTTP Response
│
▼
JSON / XML Response
REST Resource Example
Suppose an application manages users.
Resource:
/users
Retrieve all users:
GET /users
Retrieve a specific user:
GET /users/101
Create a user:
POST /users
Update a user:
PUT /users/101
Delete a user:
DELETE /users/101
Why REST APIs Are Popular
REST APIs are widely adopted because they are:
- Lightweight
- Platform-independent
- Scalable
- Easy to integrate
- Fast
- Easy to understand
- Suitable for web, mobile, and microservices
What is REST Assured?
REST Assured is a Java-based library used to automate REST API testing.
It simplifies sending HTTP requests, validating responses, and performing assertions using a fluent BDD (Behavior Driven Development) syntax.
REST Assured integrates seamlessly with TestNG, JUnit, Maven, and Jenkins, making it one of the most popular API automation tools for Java.
Interview Answer
"REST Assured is a Java library used for automating REST API testing. It provides an easy-to-read BDD-style syntax using
given(),when(), andthen(). It supports all HTTP methods, response validation, authentication, JSON/XML parsing, and integrates well with TestNG and Jenkins."
REST Assured Request Flow
given()
│
▼
when()
│
▼
then()
Example REST Assured Code
given()
.baseUri("https://reqres.in")
.when()
.get("/api/users/2")
.then()
.statusCode(200);
BDD Syntax Explained
given()
Specifies the request details.
Examples:
- Headers
- Authentication
- Query Parameters
- Request Body
when()
Specifies the HTTP request.
Examples:
- GET
- POST
- PUT
- DELETE
- PATCH
then()
Validates the response.
Examples:
- Status Code
- Response Body
- Headers
- Response Time
- Cookies
Why Use REST Assured for API Automation?
REST Assured makes API automation easier by providing a clean and readable Java API.
Key Features
Supports All HTTP Methods
- GET
- POST
- PUT
- DELETE
- PATCH
Easy Integration
Integrates with:
- TestNG
- JUnit
- Maven
- Jenkins
Response Validation
Supports validation of:
- Status Codes
- Response Body
- JSON
- XML
- Headers
- Cookies
- Response Time
Authentication Support
Supports multiple authentication mechanisms.
Examples:
- Basic Authentication
- OAuth
- Bearer Token
- API Key
Fluent BDD Syntax
REST Assured uses an easy-to-read syntax.
Example:
given()
.when()
.then();
This improves code readability and maintenance.
JSON and XML Support
REST Assured supports validating both:
- JSON responses
- XML responses
Real-Time Example
In my current project, we use REST Assured to automate REST APIs and integrate the test suite with TestNG, Maven, and Jenkins. The tests execute automatically after every code deployment, providing quick feedback and reducing manual testing effort.
Advantages of REST Assured
- Open source
- Easy to learn
- Java-friendly
- Supports BDD
- Supports JSON and XML
- Easy authentication handling
- Excellent integration with CI/CD
- Powerful response validation
SOAP vs REST
SOAP and REST are both used for communication between applications, but they differ significantly in design and usage.
Think of it like this:
- SOAP is like sending a registered parcel through a government courier with strict rules and paperwork.
- REST is like sending an email—simple, lightweight, and fast.
SOAP vs REST Comparison
| Feature | SOAP | REST |
|---|---|---|
| Type | Protocol | Architectural Style |
| Data Format | XML Only | Mostly JSON (also XML, HTML, Text) |
| Weight | Heavy | Lightweight |
| Performance | Slower | Faster |
| Flexibility | Less Flexible | Highly Flexible |
| Security | Built-in WS-Security | HTTPS, OAuth, JWT, etc. |
| Best Suited For | Banking, Financial Systems | Web Apps, Mobile Apps, Microservices |
| Learning Curve | Higher | Easier |
Example SOAP Request
<Envelope>
<Body>
<GetUser>
<id>101</id>
</GetUser>
</Body>
</Envelope>
Example REST Request
GET /users/101
Response:
{
"id":101,
"name":"John"
}
When to Use SOAP?
SOAP is preferred when applications require:
- High security
- ACID transactions
- Enterprise messaging
- Banking systems
- Government applications
When to Use REST?
REST is preferred for:
- Web applications
- Mobile applications
- Public APIs
- Cloud services
- Microservices
- E-commerce platforms
How to Verify Whether an API Is RESTful
To determine whether an API is RESTful, verify that it follows the core REST principles.
1. Resource-Based URIs
Resources should be represented using meaningful nouns.
Good Example:
/users
/orders
/products
Avoid using verbs in endpoint names.
2. Uses Standard HTTP Methods
RESTful APIs should use:
| HTTP Method | Purpose |
|---|---|
| GET | Retrieve data |
| POST | Create data |
| PUT | Update entire resource |
| PATCH | Partially update resource |
| DELETE | Remove resource |
3. Stateless Communication
Each request should contain all required information.
The server should not maintain client session information between requests.
4. Uniform Interface
The API should consistently use:
- Resource-based URIs
- Standard HTTP methods
- Standard status codes
- Predictable responses
5. Cacheability
Responses should support caching where appropriate using HTTP cache headers.
Benefits:
- Faster responses
- Reduced server load
- Better performance
6. Client-Server Separation
The client and server should be completely independent.
The client should not know implementation details of the server.
RESTful API Checklist
| Principle | Verification |
|---|---|
| Resource-Based URIs | ✔ |
| Stateless | ✔ |
| Uses Standard HTTP Methods | ✔ |
| Uniform Interface | ✔ |
| Cacheable | ✔ |
| Client-Server Architecture | ✔ |
| Layered System | ✔ |
Real-Time Interview Answer
"To verify whether an API is RESTful, I check that it follows REST principles such as resource-based URIs, standard HTTP methods, stateless communication, a uniform interface, cacheability, and proper client-server separation. If these principles are followed, the API can be considered RESTful."
Best Practices
- Use meaningful resource names.
- Follow standard HTTP methods.
- Return appropriate HTTP status codes.
- Keep APIs stateless.
- Use JSON as the default response format.
- Version APIs when introducing breaking changes.
- Secure APIs using HTTPS and authentication mechanisms such as OAuth or JWT.
Frequently Asked Questions (FAQs)
1. What is a REST API?
A REST API is an architectural style for building web services over HTTP. It exposes resources using URIs and performs operations on them using standard HTTP methods such as GET, POST, PUT, DELETE, and PATCH.
2. What are the characteristics of a REST API?
A REST API is:
- Stateless
- Client-server based
- Cacheable
- Resource-oriented
- Uses standard HTTP methods and status codes
- Supports a uniform interface
- Layered
- Supports multiple response formats (primarily JSON)
3. What is REST Assured?
REST Assured is a Java library used to automate REST API testing. It provides a fluent BDD-style syntax using given(), when(), and then(), making API test automation simple and readable.
4. Why use REST Assured for API automation?
REST Assured offers:
- Support for all HTTP methods
- Easy authentication handling
- Response validation
- JSON and XML parsing
- Integration with TestNG, JUnit, Maven, and Jenkins
- Clean and readable BDD syntax
5. What is the difference between SOAP and REST?
SOAP is a strict XML-based protocol commonly used in enterprise systems requiring high security and reliability. REST is a lightweight architectural style that typically uses JSON and is widely used for web applications, mobile applications, and microservices.
6. How do you verify whether an API is RESTful?
I verify that the API follows REST principles by checking:
- Resource-based URIs
- Standard HTTP methods
- Stateless communication
- Uniform interface
- Cacheability
- Client-server separation
- Layered architecture