How to Test a PUT API by Rest Assured






What is a PUT API?

  • PUT is an HTTP method used to update an existing resource on the server.
  • It can also be used to create a resource if it doesn’t exist, but typically it’s for updates.
  • The client sends the complete updated data to the server at a specified URL (endpoint).
  • Idempotent: Calling the same PUT request multiple times will have the same effect as calling it once.


Example scenario:
  • Suppose you have a user with id = 5.
  • A PUT request to /users/5 with new data (like updated name and job) will update the user’s information on the server.

How to Test a PUT API in Rest Assured
  • Set the Base URI: Define the server address where your API is hosted.
  • Specify the Endpoint: Use the URL of the resource you want to update.
  • Prepare the Request Body: Create a JSON object containing the updated data (like new name, job, or other attributes).
  • Send PUT Request: Send the PUT request along with the JSON body to the server.
  • Validate Response:
Status Code: Usually 200 OK or 204 No Content.
Response Body: Check if it contains the updated data or confirmation.
Headers: Verify content type, server info, etc.
  • Optional: You can perform a GET request afterward to ensure that the resource has been updated correctly.


To test a PUT API using Rest Assured in Java, you'll follow these steps:













Steps to test PUT API using Rest Assured

  • Add dependencies (Maven or Gradle)
  • Set the Base URI
  • Create the JSON request body using JSONObject
  • Send PUT request using given()
  • Validate the response

The endpoint we'll use:
https://reqres.in/api/users/2 — This updates user with ID 2.











Maven Dependency

<dependencies>
    <dependency>
        <groupId>io.rest-assured</groupId>
        <artifactId>rest-assured</artifactId>
        <version>5.3.1</version>
        <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>org.json</groupId>
        <artifactId>json</artifactId>
        <version>20230227</version>
    </dependency>
</dependencies>



Java Code using Rest Assured & JSONObject

import io.restassured.RestAssured;
import io.restassured.response.Response;
import org.json.JSONObject;

import static io.restassured.RestAssured.given;

public class PutApiTest {

    public static void main(String[] args) {

        // Step 1: Set Base URI
        RestAssured.baseURI = "https://reqres.in/api";

        // Step 2: Create JSON body using JSONObject
        JSONObject requestBody = new JSONObject();
        requestBody.put("name", "Himanshu");
        requestBody.put("job", "Software Engineer");

        // Step 3: Send PUT request and get the response
        Response response = given()
                .header("Content-Type", "application/json")
                .body(requestBody.toString())
                .when()
                .put("/users/2")
                .then()
                .statusCode(200)  // Verifying status code
                .extract()
                .response();

        // Step 4: Print response
        System.out.println("Response Body:");
        System.out.println(response.getBody().asPrettyString());
    }
}



Code Explanation:

LinePurpose
RestAssured.baseURISets the base server endpoint
JSONObjectBuilds the request payload
given().header().body()Prepares the request with header & body
.put("/users/2")Sends PUT request to /users/2
.statusCode(200)Asserts response code is 200 OK
.response()Extracts full response
.asPrettyString()Converts raw response to formatted JSON string


Response:

Put respond with status code of 204 or 200, that means API executes successfully and performed update operation.

{
    "name": "Himanshu",
    "job": "Software Engineer",
    "updatedAt": "2025-07-28T13:48:32.054Z"
} 



Suggested Posts:

1. Test Digest Auth by RestAssured
2. How to extract Response in Rest Assured by JSONPath
3. Test OAth2 in RestAssured
4. Validate Request and Response in POJO by RestAssured
5. How to Test Basic Authentication in RestAssured