Understanding RESTful APIs

Before we dive into using REST Assured, it's important to understand what RESTful APIs are and how they work.

What is an API?

An API (Application Programming Interface) allows different software applications to talk to each other. It acts like a messenger delivering your request to a system and bringing the system's response back to you.

For example:

  • When you check the weather on your phone, the app uses an API to fetch weather data from a server.
  • When you order food online, the app communicates with a restaurant’s system through an API.

What is a RESTful API?

A RESTful API is an API that follows REST principles. REST stands for Representational State Transfer

It's a common way to design APIs that are:

  • Simple to use
  • Fast and scalable
  • Stateless (Each request is independent—no data is stored between requests.)

How Does a REST API Work?

Think of a REST API as a waiter in a restaurant:

  • You place an order (Request).
  • The kitchen prepares your food (Server processing the request).
  • The waiter brings your food (Response).

In technical terms:

  • You (the client) send a request to a server.
  • The server processes the request.
  • The server sends a response back to you.

Understanding Key REST API Concepts

  1. Client and Server
    • Client: The application sending the request (e.g., a web browser or mobile app).
    • Server: The system receiving and responding to the request (e.g., a database or backend system).
  2. Resources

    A resource is any object you want to interact with—like a book, product, or order. Each resource is identified by a unique URL.
    Example:

    • http://192.xxx.xx.xxx:8080/books (List of books)
    • http://192.xxx.xx.xxx:8080/books/20250401 (Details of book with isbn 20250401)

Common HTTP Methods in REST APIs

REST APIs use HTTP methods to perform different actions on resources. Here are the most common ones:

HTTP Method Purpose Example
GET Retrieve data (Read) Get book information
POST Create new data (Write) Add a new book
PUT Update existing data (Replace) Update all book details
PATCH Modify part of data (Update) Update specific book info
DELETE Remove data (Delete) Delete a book record

Example URLs:

  • GET /books → Get all books
  • POST /books → Create a new book
  • GET /books/123 → Get book with ID 123
  • PUT /books/123 → Update books with ID 123
  • DELETE /books/123 → Delete books with ID 123

HTTP Status Codes

When you interact with a REST API, the server responds with an HTTP status code to tell you if the request was successful or if something went wrong.

Status Code Meaning Example
200 OK (Request succeeded) Data retrieved successfully
201 Created (Resource added) New book created
400 Bad Request (Invalid input) Missing required data
401 Unauthorized (Not allowed) Invalid API key
404 Not Found (Resource missing) Book doesn't exist
500 Internal Server Error Server malfunction

Example REST API Request and Response

Let's say we want to get details about a book from a books library API.

Request (GET method)

GET http://1xx.xxx.xx.xxx:8080/books/20250401

Response (JSON format)

JSON
Copy
{
    "price": 100.0,
    "isbn": "20250401",
    "name": "Book 1",
    "publisher": "ABC publisher",
    "language": "English"
}

Why Do Testers Use REST Assured for APIs?

When building or testing software, it’s important to verify that APIs:

  • Work correctly (return the right data).
  • Handle errors properly.
  • Follow security rules (like authentication).

REST Assured helps us automate these checks by writing easy-to-understand test scripts.