As a part of End to End REST Assured Tutorial , in this post We will write first PATCH request in REST assured.
I will strongly recommended you to learn basics of API testing from here.
To learn what is PATCH request and how can you hit a PATCH request in Postman tool, must refer this post.
We will update partially a booking details using “UpdateBooking” API provided by Restful Booker.
Refer Update Booking API documentation to understand how authentication and body need to be provided to request.
Below points summarize the above documentation :-
- We need to pass booking id for which you want to update details in URI. E.g. https://restful-booker.herokuapp.com/booking/1 where “1” is booking id.
- Authentication token need to pass as cookie. Cookie name is “token” and value is generated auth token. ‘Cookie: token=<generatedToken>’.
- Pass the fields which you want to update in body with new value. Remember it is a PATCH request where you no need to pass whole body.
Let’s see existing details of a Booking ID 1 using Postman:-
Let’s generate token which I have already covered in Previous post. Perform all above steps in Postman tool as of now. We will see in upcoming posts to chain different requests in REST Assured.
Generate Token:-
Let’s write PATCH request in REST Assured:-
Updated Request Body:-
I have just updated firstName and lastName. So in request payload, we will send only two fields unlike PUT in previous post.
{ "firstname" : "Amod", "lastname" : "Mahajan" }
REST Assured Code:-
Non-BDD Style:-
package RestfulBooker.patchExamples; import org.hamcrest.Matchers; import io.restassured.RestAssured; import io.restassured.http.ContentType; import io.restassured.response.Response; import io.restassured.response.ValidatableResponse; import io.restassured.specification.RequestSpecification; public class NonBDDPatchRequest { public static void main(String[] args) { // There is no need to add escape character manually. Just paste string within // double // quotes. It will automatically add escape sequence as required. String jsonString = "{\r\n" + " \"firstname\" : \"Amod\",\r\n" + " \"lastname\" : \"Mahajan\"}"; // Create a request specification RequestSpecification request = RestAssured.given(); // Setting content type to specify format in which request payload will be sent. // ContentType is an ENUM. request.contentType(ContentType.JSON); // Setting a cookie for authentication as per API documentation request.cookie("token", "974c253452ba510"); // Adding URI request.baseUri("https://restful-booker.herokuapp.com/booking/1"); // Adding body as string request.body(jsonString); // Calling PATCH method on URI. After hitting we get Response Response response = request.patch(); // Printing Response as string System.out.println(response.asString()); // Get Validatable response to perform validation ValidatableResponse validatableResponse = response.then(); // Validate status code as 200 validatableResponse.statusCode(200); // Validate value of firstName is updated validatableResponse.body("firstname", Matchers.equalTo("Amod")); // Validate value of lastName is updated validatableResponse.body("lastname", Matchers.equalTo("Mahajan")); } }
BDD Style:-
package RestfulBooker.patchExamples; import org.hamcrest.Matchers; import io.restassured.RestAssured; import io.restassured.http.ContentType; import io.restassured.response.Response; import io.restassured.response.ValidatableResponse; import io.restassured.specification.RequestSpecification; public class BDDStylePatchRequest { public static void main(String[] args) { // There is no need to add escape character manually. Just paste string within // double // quotes. It will automatically add escape sequence as required. String jsonString = "{\r\n" + " \"firstname\" : \"Amod\",\r\n" + " \"lastname\" : \"Mahajan\"}"; //GIVEN RestAssured .given() .baseUri("https://restful-booker.herokuapp.com/booking/1") .cookie("token", "6608dc75eedd44f") .contentType(ContentType.JSON) .body(jsonString) // WHEN .when() .patch() // THEN .then() .assertThat() .statusCode(200) .body("firstname", Matchers.equalTo("Amod")) .body("lastname", Matchers.equalTo("Mahajan")); } }
Note:- You need to generate access token every time you hit the request otherwise you will get error code as 403.
You can clone/download example repo here.
If you have any doubt, feel free to comment below.
If you like my posts, please like, comment, share and subscribe.
#ThanksForReading
#HappyLearning
You can find all Selenium related post here.
You can find all API manual and automation related posts here.
You can find frequently asked Java Programs here.
Not working above URI
Hi,
How to update nested json value using patch
for example
{
“bookingdetails” :{
“checkin” : “2021-06-19”,
“checkout” : “2021-06-25”
}
}
how to update values for checkin and chekout??
Thank you for this great series.. where we can create all using the same example
how to generate access token? will developer provide token each time ?
you can write a function which will return it. Here its coming from a different post API