Rest Assured PUT Example
PUT is one of the most common methods of HTTP which is used to send data to a server to update an existing resource and if the requested URI doesn’t exist then it will create a new resource. The difference between POST and PUT is, POST will return status code 201, and PUT will return 200.
Important points of PUT requests:
- PUT methods cannot be cached
- PUT method is idempotent which means that calling the same PUT request multiple times will always produce the same result.
- PUT method is called when you have to modify a single resource.
- PUT requests parameter data is unlimited as there are no length restrictions
- PUT requests allow ASCII characters
- PUT requests are used to send data to the server to update an existing resource or create a new resource.
Rest Assured PUT Example
API | /user{username} |
HTTP method Type | PUT |
Body | { “id”: 0, “username”: “string”, “firstName”: “string”, “lastName”: “string”, “email”: “string”, “password”: “string”, “phone”: “string”, “userStatus”: 0 } |
Response body | { “code”: 200, “type”: “unknown”, “message”: “1327” } |
Response header | access-control-allow-headers: Content-Typeapi_keyAuthorization access-control-allow-methods: GETPOSTDELETEPUT access-control-allow-origin: * content-type: application/json date: Sun02 Aug 2020 07:20:38 GMT server: Jetty(9.2.9.v20150224) status: 200 |
How to write PUT request API Test using Rest Assured?
1. Prepare input data fields JSON Object
JSONObject user = new JSONObject();
user.put("id", 295);
user.put("username", "user1");
user.put("firstName","Tani");
user.put("lastName", "Mehta");
user.put("email", "[email protected]");
user.put("password", "abccd");
user.put("phone", "1234567890");
user.put("userStatus", 1);
Here we are creating a JSON object to pass it into the request body of the post request.
2. Set up a request with the specified base URI
RestAssured.baseURI = "https://petstore.swagger.io/v2/user";
Base URI is the root address of the Resource. Here we are specifying to REST assured to use the above as the base URI of the service.
3. Specify the exact resource and perform a PUT request to it
RestAssured.given()
contentType("application/json")
.body(map)
.when()
.put("/user1")
In the Given statement the JSON object created in step 1 is sent into the request body. The API to create the user is /user, which is send in the POST call and would be appended to the base URI. Thus, the complete URL would be “https://petstore.swagger.io/v2/user” to which we are sending a POST request with JSON body. Here we are updating the existing record of “user1”. Here an important point to be noted that if the existing record which is “user1” in our case, does not exist then the PUT method will create a new record.
4. Response Validation
Here we usually perform below two types of validation:
1. Validation of Status code
.then().assertThat().statusCode(201);
Since it is a POST request for which we are making a call, thus the expected status code would be 201. Here, we are validating the response based on the condition where if status code from the response matches with the value 201.
2. Validation of Response header
.then().assertThat().header("content-type", "application/json");
Here, we are verifying if the content-type matches “application/json” then the assert will return true else false.
Complete Example Code
@Test
public void UpdateUserDetail() {
JSONObject user = new JSONObject();
user.put("id", 295);
user.put("username", "user1");
user.put("firstName","Tani");
user.put("lastName", "Mehra");
user.put("email", "[email protected]");
user.put("password", "abcd");
user.put("phone", "1234567890");
user.put("userStatus", 1);
RestAssured.baseURI = "https://petstore.swagger.io/v2/user";
RestAssured.given()
contentType("application/json")
.body(map)
.when()
.put("/user1")
.then().assertThat().statusCode(200);
}
Difference between POST and PUT request
POST | PUT |
POST perform create operation | PUT performs update as well as create operation |
POST method is used when a new record has to be created | PUT method is used when you have to modify a single resource. |
POST request is non-idempotent | PUT request is idempotent |
POST return status code 200 | PUT return status code 201 |