Testing REST API with the HTTP Methods

Wina Aprilia
Bento Tech Innovation
6 min readJul 20, 2023

What is API?

What is API? API stands for Application Programming Interface. API is a collection of specific functions that enable different applications to interact with each other. API interacts through requests and responses between the client and server. API is commonly used in software development, particularly in the creation of web and mobile applications.

What is REST API?

REST stands for Representational State Transfer. REST (Representational State Transfer) API is an architectural style used in web application development. REST uses the HTTP protocol to send requests and receive responses between clients and servers. The REST API allows developers to create, read, update, and delete data through CRUD (Create, Read, Update, Delete) operations implemented via HTTP methods. The HTTP method itself consists of GET, POST, PUT, DELETE, etc. For its function, GET is to display data, POST is to add data, PUT is to change data, and DELETE is to delete data. For the tools, there are several tools that can be used for performing REST API testing, such as Postman, Insomnia, SoapUI, etc.

How to Testing REST API with HTTP Methods

When testing REST APIs, it’s important to test the API’s behavior and functionality using various HTTP methods. Here’s an overview of how to test REST APIs with different HTTP methods:

1. GET Method
• Send GET requests to retrieve all data.
• Send GET requests to retrieve detailed data with specific unique code in endpoint.
• Verify that the API retrieves the data successfully and returns with response code 200 OK.
• Verify that the API returns the amount of total data and list data.
• Validate data by filter parameter.
• Validate data by sorting data by ascending/descending.
• Verify when retrieve data detail with id not exist displayed response error not found.
• Verify when filter by pagination and limit data displayed properly.
For example, we send a GET request to display all data users in page number 1

Sample GET method

For example, we send a GET request to display detailed data user with id 5

Sample GET (detail) method

2. POST Method
• Send POST requests to create new.
• Input valid input data in the request body.
• Verify that the API creates the data successfully and returns with response code 201 OK.
• Verify when input request body with empty mandatory displayed response error.
• Verify when input request body with invalid input will return response code error.
• To make sure retrieve the data with a GET detail request to ensure that data is already created.
For example, we send a POST request to create data user

Sample POST method

3. PUT Method
• Send PUT requests to update existing data with input unique code in endpoint.
• Input the updated data with valid input in the request body.
• Verify that the API update the data successfully and return with response code 200 OK.
• Verify when update data in request body with empty mandatory displayed response error.
• Verify when update data in request body with invalid input displayed response error.
• Verify when update data with id not exist displayed response error not found.
• To make sure retrieve the data with a GET detail request to ensure that data is already updated.
For example, we send a PUT request to update data user

Sample PUT method

4. DELETE Method
• Send DELETE requests to remove data with input unique code in endpoint.
• Verify that the API remove the data successfully and return with response code 200 OK.
• Verify when delete data with id not exist displayed response error not found.
• Confirm that the data is no longer accessible.
• To make sure retrieve the data with a GET detail request to ensure that data is no longer exists.
For example, we send a DELETE request to remove data user

Sample DELETE method

During the test, things that also need to be checked are header token authentication, invalid input (for example: input parameter name with numeric), and other error responses that are checked in each HTTP method. In addition, do also test with a variety of scenarios negative.

The following are the differences between API testing and other types of testing:

Implementation Testing REST API with the HTTP methods in BerandaToko

In BerandaToko there is an API Contract in Technical Requirement Documentation. Also known as an API documentation consisting of HTTP methods, endpoint, query params, request body, response field, insomnia collection, and error handling. Here are some steps for testing REST API with the HTTP methods in BerandaToko:

1. Install tools
There are various tools for testing REST API based on your preferences and requirements. In BerandaToko using insomnia tools for testing REST API.

2. Import the API collections
After install the tools, import the insomnia collection from API Contract in TRD.

Sample collection API

3. Create test case
Create test case positive and negative scenarios based on API Contract in TRD.

Sample list test cases and test results
Sample detail test case

4. Test all method HTTP in API Contract with positive and negative scenarios
In BerandaToko are mainly 4 methods involve to testing like GET, POST, DELETE, and PUT.
For example, creating a CRUD for a discount.

  • GET Method : GET method is used to retrieve the list discount.
    Scenario positive: User wants to view list discount with in date range discount.
    Action: Send request with GET method with valid data.
    Scenario negative: User wants to view list discount with out-of-range date discount period.
    Action: Send request in GET method with date range in 2025-01-01 until 2025-01-02.
  • POST Method : POST method is used to create a new data discount.
    Scenario positive: User wants to add new data discount with valid data.
    Action: Send request in POST method with all mandatory complete.
    Scenario negative: User wants to add duplicate data discount in the existing date range. Action: Send request in POST method with duplicate data existing.
    For example, we send a POST request to create data discount based on API contract.
Sample request body fields
Sample endpoint, request, and response
  • PUT Method: PUT method is used to update the data discount.
    Scenario positive: User wants to update data discount with valid data.
    Action: Send request in PUT method with all mandatory complete.
    Scenario negative: User wants to update data discount with mandatory incomplete.
    Action: Send request in PUT method with mandatory field incomplete.
  • DELETE Method: DELETE method is used to remove the data discount.
    Scenario positive: User wants to remove data discount with valid data.
    Action: Send request in DELETE method with data discount existing.
    Scenario negative: User wants to remove data with data already deleted. Action: Send request in DELETE method with data already deleted.

Overall testing REST API with the HTTP methods, you can ensure that the API is as expected with the requirement. In BerandaToko testing REST API with the HTTP methods ensure the API Contract in TRD is expected and acceptable with the requirement of team product based on API Contract in Technical Requirement Documentation.

Reference:
https://aws.amazon.com/id/what-is/api/?nc1=h_ls
https://testfully.io/blog/http-methods/
https://www.guru99.com/testing-rest-api-manually.html
https://reqres.in/

--

--