</>

Technology

Rest Assured

Difficulty

Beginner

Interview Question

How to automate the PUT method in Rest Assured?

Answer

Automating PUT Method in Rest Assured

A PUT method puts or places a file or resource precisely at a specific URI. In case a file or resource already exists at that URI, PUT replaces it. If there is no file or resource, PUT creates a new one.

PUT with JSON Body from File

Java
@Test(description = "Automate PUT method and validate response")
public void methodValidationPUT() throws IOException, ParseException {

    FileInputStream file = new FileInputStream(
        new File(System.getProperty("user.dir") + "\\TestData\\put.json")
    );

    Response resp = given()
                        .header("Content-Type", "application/json")
                        .body(IOUtils.toString(file, "UTF-8"))
                        .when()
                        .put("https://reqres.in/api/users/2");

    assertEquals(resp.getStatusCode(), 200);
    assertEquals(resp.path("job"), "tester");
}

PUT with Inline JSON

Java
@Test
public void updateUserPUT() {

    String requestBody = "{ \"name\": \"Updated John\", \"job\": \"Senior QA\" }";

    given()
        .contentType(ContentType.JSON)
        .body(requestBody)
        .when()
        .put("https://reqres.in/api/users/2")
        .then()
        .statusCode(200)
        .body("name", equalTo("Updated John"))
        .body("job", equalTo("Senior QA"));
}

PUT with Path Parameter

Java
@Test
public void putWithPathParam() {
    int userId = 2;
    String body = "{ \"name\": \"Jane\", \"job\": \"SDET\" }";

    given()
        .contentType(ContentType.JSON)
        .pathParam("id", userId)
        .body(body)
        .when()
        .put("/api/users/{id}")
        .then()
        .statusCode(200);
}

PUT vs PATCH

FeaturePUTPATCH
UpdatesEntire resourcePartial fields only
IdempotentYesNo
Missing fieldsSet to null/defaultLeft unchanged
Status on success200 OK200 OK

Follow AutomateQA

Related Topics