Different ways to pass body in post request Rest Assured – Part 9

Different ways to pass body in post request Rest Assured

What we have learned so far?

Part 1 – What is Rest Assured? How to setup Rest Assured?
https://www.onlyfullstack.com/what-is-rest-assured-how-to-setup-rest-assured/

Part 2 – Sample Rest API To Test With Rest Assured
https://www.onlyfullstack.com/sample-rest-api-to-test-with-rest-assured/

Part 3 – How to make a GET Request using Rest Assured – Only Fullstack
https://www.onlyfullstack.com/how-to-send-get-request-in-rest-assured/

Part 4 – How to make a POST Request using Rest Assured – Only Fullstack
https://www.onlyfullstack.com/how-to-send-post-request-in-rest-assured/

Part 5 – How to make a PUT Request using Rest Assured – Only Fullstack
https://www.onlyfullstack.com/how-to-send-put-request-in-rest-assured/

Part 5 – How to make a DELETE Request using Rest Assured – Only Fullstack
https://www.onlyfullstack.com/how-to-send-delete-request-in-rest-assured/

Part 7 – JsonPath and XmlPath in Response Rest Assured – Only Fullstack
https://www.onlyfullstack.com/jsonpath-and-xmlpath-in-rest-assured/

Part 8 – Getting and Verifying Response in Rest Assured – Only Fullstack
https://www.onlyfullstack.com/getting-and-verifying-response-in-rest-assured/
 

Different ways to pass body in post request Rest Assured

Rest apis can take the input body for POST and PUT in Json or in XML. We can provide the body in different ways. Lets explore these ways of rest assured post body 

 

1. Request Body as Json in Rest Assured

we can pass the body as json string in rest assured.

@Test
public void postBodyWithJsonString() {
    String requestBody = " {n" +
            ""firstName": "Elon1",n" +
            ""lastName": "Musk1",n" +
            ""salary": 3000,n" +
            ""email": "elonmusk@abc.com"n" +
            "}";

    RestAssured.given()
            .baseUri("http://localhost:8088")
            .body(requestBody)
            .contentType(ContentType.JSON)
            .when()
            .post("/employees")
            .then()
            .statusCode(201)
            .body("id", Matchers.notNullValue())
            .body("firstName", Matchers.equalTo("Elon1"))
            .body("lastName", Matchers.equalTo("Musk1"))
            .body("salary", Matchers.equalTo(3000))
            .body("email", Matchers.equalTo("elonmusk@abc.com"));
}

2. Request Body as Map in Rest Assured

we can pass the body as key value pair(map) in rest assured.

@Test
public void postBodyWithMap() throws IOException {
    // We can pass the request as json string
    // String expectedJsonResponse = new String(Files.readAllBytes(Paths.get("src/test/resources/post-request-request-employee.json")));

    Map<String, Object>  employeeJsonAsMap = new HashMap<>();
    employeeJsonAsMap.put("firstName", "Jack");
    employeeJsonAsMap.put("lastName", "Ma");
    employeeJsonAsMap.put("salary", 1000);
    employeeJsonAsMap.put("email", "jack@gmail.com");

    RestAssured.given()
            .baseUri("http://localhost:8088")
            .body(employeeJsonAsMap)
            .contentType(ContentType.JSON)
            .when()
            .post("/employees")
            .then()
            .statusCode(201)
            .body("id", Matchers.notNullValue())
            .body("firstName", Matchers.equalTo(employeeJsonAsMap.get("firstName")))
            .body("lastName", Matchers.equalTo(employeeJsonAsMap.get("lastName")))
            .body("salary", Matchers.equalTo(employeeJsonAsMap.get("salary")))
            .body("email", Matchers.equalTo(employeeJsonAsMap.get("email")));
}

3. Request Body as Object in Rest Assured

we can pass the body as an object in rest assured.

@Test
public void postBodyWithObject() {
    Employee inputBody = new Employee("only", "fullstack", 2000, "onlyfullstack@abc.com");

    RestAssured.given()
            .baseUri("http://localhost:8088")
            .body(inputBody)
            .contentType(ContentType.JSON)
            .when()
            .post("/employees")
            .then()
            .statusCode(201)
            .body("id", Matchers.notNullValue())
            .body("firstName", Matchers.equalTo("only"))
            .body("lastName", Matchers.equalTo("fullstack"))
            .body("salary", Matchers.equalTo(2000))
            .body("email", Matchers.equalTo("onlyfullstack@abc.com"));
}

4. Request Body from file in Rest Assured

we can pass the body as object which is read from the file in rest assured. ObjectMapper is used to convert the Json data into specified object format.

@Test
public void postBodyWithJsonFromFile() throws IOException {
    // We can pass the request as json string
    // String expectedJsonResponse = new String(Files.readAllBytes(Paths.get("src/test/resources/post-request-request-employee.json")));

    ObjectMapper mapper = new ObjectMapper();
    Employee request = mapper.readValue(new ClassPathResource("post-request-input-employee.json").getFile(), Employee.class);

    Employee response = RestAssured.given()
            .baseUri("http://localhost:8088")
            .body(request)
            .contentType(ContentType.JSON)
            .when()
            .post("/employees")
            .then()
            .extract()
            .as(Employee.class);
    Assert.assertEquals(response, request);
            /*.then()  // BDD approach with comparing individual fields
            .statusCode(201)
            .body("id", Matchers.notNullValue())
            .body("firstName", Matchers.equalTo("Sachin"))
            .body("lastName", Matchers.equalTo("Tendulkar"))
            .body("salary", Matchers.equalTo(2000))
            .body("email", Matchers.equalTo("sachin@abc.com"));*/
}
@Test
public void postBodyWithJsonFromFile() throws IOException {
// We can pass the request as json string
// String expectedJsonResponse = new String(Files.readAllBytes(Paths.get("src/test/resources/post-request-request-employee.json")));

ObjectMapper mapper = new ObjectMapper();
Employee request = mapper.readValue(new ClassPathResource("post-request-input-employee.json").getFile(), Employee.class);

Employee response = RestAssured.given()
.baseUri("http://localhost:8088")
.body(request)
.contentType(ContentType.JSON)
.when()
.post("/employees")
.then()
.extract()
.as(Employee.class);
Assert.assertEquals(response, request);
/*.then() // BDD approach with comparing individual fields
.statusCode(201)
.body("id", Matchers.notNullValue())
.body("firstName", Matchers.equalTo("Sachin"))
.body("lastName", Matchers.equalTo("Tendulkar"))
.body("salary", Matchers.equalTo(2000))
.body("email", Matchers.equalTo("sachin@abc.com"));*/
}

Lets go to our next tutorial where we will discuss below points :

Part 10 – How to test OAuth2 protected api authentication in Rest Assured

  – How the Authorization happens in Rest APIs?
 – What is OAuth Authentication used for Rest API? What are the advantages of this Authorization System?
 – Why can’t we send username and password in each rest call?
 – How to call the Rest API which Require Authentication in Postman?
 – How to get the OAuth2 Authentication Bearer in Rest Assured?
 – How to get the OAuth2 Access Token in Rest Assured?
– How to call the OAuth2 protected Rest api in Rest Assured?
https://www.onlyfullstack.com/authentication-in-rest-assured/

Source Code
You can find the complete source code on below GitHub repository – 
https://github.com/onlyfullstack/rest-assured-tutorial

Rest Assured Tutorial
https://www.onlyfullstack.com/rest-assured-tutorial-for-beginners/