
Put and Patch method in Rest Assured
Difference Between Put and Patch Method
What is Put?
When creating an object or resource on an HTTP server, clients utilize the PUT method. This setup procedure itself may take one of two shapes:
When an object is requested that doesn’t already exist, the server creates it and returns a response code 201 to the client.
The server changes the entity, which already exists, and returns a success code of 200 and 204 to a client. Moreover, the server should provide the client with the appropriate error number, often a 4xx or 5xx, if a PUT request encounters a problem.
What is Patch?
The PATCH technique modifies resource elements only partially. The requested modifications are executed atomically via the PATCH technique. It implies that the server won’t alter the target object if it can’t accommodate all of the requested modifications. In this approach, if the request is carried out correctly, the server sends the client the success code 204. Otherwise, an error code is sent by the server.
Here is simple example in Rest Assured for Put and Patch
package session02;
import org.json.simple.JSONObject;
import org.testng.annotations.Test;
import io.restassured.RestAssured;
import io.restassured.http.ContentType;
public class TestPutMethod {
@Test
public void test04() {
JSONObject jsonData = new JSONObject();
jsonData.put("Name", "Rahul kundu");
jsonData.put("job", "manager");
RestAssured.baseURI = "https://reqres.in/api/users/52";
RestAssured.given().header("Content-Type","application/json").contentType(ContentType.JSON)
.body(jsonData.toJSONString())
.when().put()
.then().statusCode(200).log().all();
}
@Test
public void test05() {
JSONObject jsonData = new JSONObject();
jsonData.put("Name", "Rahul kundu");
jsonData.put("job", "QA manager");
RestAssured.baseURI = "https://reqres.in/api/users/52";
RestAssured.given().header("Content-Type","application/json").contentType(ContentType.JSON)
.body(jsonData.toJSONString())
.when().patch()
.then().statusCode(200).log().all();
}
}