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
| Feature | PUT | PATCH |
|---|---|---|
| Updates | Entire resource | Partial fields only |
| Idempotent | Yes | No |
| Missing fields | Set to null/default | Left unchanged |
| Status on success | 200 OK | 200 OK |
