- 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 body with all details. Remember it is a PUT request which takes whole body like POST request.
Let’s see existing details of a Booking ID 1 using Postman:-
Generate Token:-
Let’s write PUT request in REST Assured:-
Updated Request Body:-
I have just updated firstName and lastName.
{
"firstname" : "Amod",
"lastname" : "Mahajan",
"totalprice" : 111,
"depositpaid" : true,
"bookingdates" : {
"checkin" : "2018-01-01",
"checkout" : "2019-01-01"
},
"additionalneeds" : "Breakfast"
}
REST Assured Code:-
Non-BDD Style:-
package RestfulBooker.putExamples;
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 NonBDDPutRequest {
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\",\r\n"+ " \"totalprice\" : 111,\r\n" + " \"depositpaid\" : true,\r\n" + " \"bookingdates\" : {\r\n"+ "\"checkin\" : \"2018-01-01\",\r\n" + " \"checkout\" : \"2019-01-01\"\r\n"+ " },\r\n" + " \"additionalneeds\" : \"Breakfast\"\r\n" + "}";
// 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", "fa0d5b1138a0d1f");
// Adding URI
request.baseUri("https://restful-booker.herokuapp.com/booking/1");
// Adding body as string
request.body(jsonString);
// Calling PUT method on URI. After hitting we get Response
Response response = request.put();
// 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.putExamples;
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 BDDStylePutRequest {
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\",\r\n"
+ " \"totalprice\" : 111,\r\n" + " \"depositpaid\" : true,\r\n" + " \"bookingdates\" : {\r\n"
+ " \"checkin\" : \"2018-01-01\",\r\n" + " \"checkout\" : \"2019-01-01\"\r\n"
+ " },\r\n" + " \"additionalneeds\" : \"Breakfast\"\r\n" + "}";
//GIVEN
RestAssured
.given()
.baseUri("https://restful-booker.herokuapp.com/booking/1")
.cookie("token", "e88375c0fde687a")
.contentType(ContentType.JSON)
.body(jsonString)
// WHEN
.when()
.put()
// 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.