Table of Contents
How to send POST Request in Rest Assured
What we have learned so far?
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/
What is HTTP POST request?
In computing, POST is a request method supported by HTTP used by the World Wide Web. By design, the POST request method requests that a web server accepts the data enclosed in the body of the request message, most likely for storing it. In our employee rest api you can hit the Save Employee request to save the details of the employees. For this you need to pass the body in you POST request
Save Employee –
HTTP Method – POST
URL – http://localhost:8088/employees
URL Parameters – No Parameters Required
Body –
{
“firstName” : “Only”,
“lastName” : “FullStack”,
“email” : “onlyfullstack@gmail.com”,
“salary” : “10000”
}
Response – Returns the saved employee
How to make a POST Request using Rest Assured?
Lets write code to automate this http post request in rest assured and check the output –
http post request in rest assured automation without BDD approach –
@Test public void postMethodWithoutBDDApproach() { String requestBody = " {n" + " "firstName": "Elon",n" + " "lastName": "Musk",n" + " "salary": 3000,n" + " "email": "elonmusk@abc.com"n" + " }"; RequestSpecification request = RestAssured.given(); request.baseUri("http://localhost:8088"); request.body(requestBody); request.contentType(ContentType.JSON); Response response = request.post("/employees"); // Let's print response body. String resString = response.asString(); System.out.println("Respnse Details : " + resString); /* * To perform validation on response like status code or value, we need to get * ValidatableResponse type of response using then() method of Response * interface. ValidatableResponse is also an interface. */ ValidatableResponse valRes = response.then(); // It will check if status code is 201 valRes.statusCode(201); // It will check if status line is as expected valRes.body("id", Matchers.notNullValue()); valRes.body("firstName", Matchers.equalTo("Elon")); valRes.body("lastName", Matchers.equalTo("Musk")); valRes.body("salary", Matchers.equalTo(3000)); valRes.body("email", Matchers.equalTo("elonmusk@abc.com")); }
As we have seen in the http post request method with Rest Assured we have explored all the lines shown above. The only difference we are doing
1. Set URI
First we need to set the base uri of our api. uri is the base url without the resource. means in our case the complete url is http://localhost:8088/employees and here the uri is http://localhost:8088/ and the rescue name is employee.
RequestSpecification request = RestAssured.given(); request.baseUri("http://localhost:8088/");
2. Call post method
Before calling the post method we should pass the body(json of the employee we want to save).
RestAssured has a static overloaded method named post(String resourceName) which returns a reference of Response interface. In fact return type of all http methods in RestAssured class is of type Response. This response contains every details returned by hitting request i.e. response body, response headers, status code, status lines, cookies etc.
request.body(requestBody); request.contentType(ContentType.JSON); Response response = request.post("/employees");
3. Validate the response
To validate response like status code or value , we need to get reference of type ValidatableResponse. ValidatableResponse is an interface. Response interface has a method named “then()” which returns ValidatableResponse. In fact there is an interface called “Validatable” which has “then()” method. Response interface extends
Once we get ValidatableResponse reference, we can do many assertions. In this post, we will verify status code and status line. It consists of many validation methods.
In below code we are first validating the status code and then we are validating the saved employee records and comparing all the properties with expected value. We are using the JsonPath to retrieve the value from the response json.
Don’t worry about the JsonPath. We will learn about them in our upcoming blog.
body method takes 2 parameters, first is the json path for which the value you have to compare and second one is the Matcher which has lots of method to compare the value.
T body(String var1, Matcher<?> var2, Object... var3); /* * To perform validation on response like status code or value, we need to get * ValidatableResponse type of response using then() method of Response * interface. ValidatableResponse is also an interface. */ ValidatableResponse valRes = response.then(); // It will check if status code is 200 valRes.statusCode(201); // It will check if status line is as expected valRes.body("id", Matchers.notNullValue()); valRes.body("firstName", Matchers.equalTo("Elon")); valRes.body("lastName", Matchers.equalTo("Musk")); valRes.body("salary", Matchers.equalTo(3000)); valRes.body("email", Matchers.equalTo("elonmusk@abc.com"));
http post request in rest assured api automation with BDD approach
BDD is an agile software development process (Not a Software Testing process) which defines expectation from an application to behave from intended user prospective. Don’t worry about the BDD we will discuss in the upcoming blog.
Each scenario is written in form of
- Given (Pre-conditions/ Context),
- When (Action/Event Performed) and
- Then (Result/Outcomes) format.
@Test public void postMethodWithBDDApproach() { 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")); }
Below is the complete program for HTTP post method with rest assured
package onlyfullstack.httpMethods; import io.restassured.RestAssured; import io.restassured.http.ContentType; import io.restassured.response.Response; import io.restassured.response.ValidatableResponse; import io.restassured.specification.RequestSpecification; import org.hamcrest.Matchers; import org.testng.annotations.Test; public class PostHttpMethodExample { @Test public void postMethodWithoutBDDApproach() { String requestBody = " {n" + ""firstName": "Elon",n" + ""lastName": "Musk",n" + ""salary": 3000,n" + ""email": "elonmusk@abc.com"n" + "}"; RequestSpecification request = RestAssured.given(); request.baseUri("http://localhost:8088"); request.body(requestBody); request.contentType(ContentType.JSON); Response response = request.post("/employees"); // Let's print response body. String resString = response.asString(); System.out.println("Respnse Details : " + resString); /* * To perform validation on response like status code or value, we need to get * ValidatableResponse type of response using then() method of Response * interface. ValidatableResponse is also an interface. */ ValidatableResponse valRes = response.then(); // It will check if status code is 201 valRes.statusCode(201); // It will check if status line is as expected valRes.body("id", Matchers.notNullValue()); valRes.body("firstName", Matchers.equalTo("Elon")); valRes.body("lastName", Matchers.equalTo("Musk")); valRes.body("salary", Matchers.equalTo(3000)); valRes.body("email", Matchers.equalTo("elonmusk@abc.com")); } @Test public void postMethodWithBDDApproach() { 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")); } }
Lets go to our next tutorial where we will discuss below points :
Part 5 – How to make a PUT Request using Rest Assured
– What is PUT?
– How to make a PUT Request using Rest Assured?
– Rest assured http put request api automation without BDD approach
– Rest assured get api automation with BDD approach
https://www.onlyfullstack.com/how-to-send-put-request-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/