Rest Assured POST Example
POST is one of the most common methods of HTTP which is used to send data to a server to create the new resource. The data is sent to the server is either in the form of Request Body or Request Parameters.
Important points of POST requests:
- POST requests parameters don’t get stored in the browser history
- POST requests are used to send data to the server to create the resource
- POST requests cannot be cached
- POST requests are secure as compared to GET because parameters or data doesn’t get stored in the browser history
- POST requests parameter data is unlimited as there are no length restrictions
- POST requests allow ASCII characters
- POST requests are difficult to hack
- HttpClient API provides a class named HttpPost which represents the post request method.
Rest Assured POST Example
API | /user |
HTTP method Type | POST |
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 POST 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", "Mehra");
user.put("email", "[email protected]");
user.put("password", "abcd");
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/";
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 POST request to it
RestAssured.given()
contentType("application/json")
.body(map)
.when()
.post("/user")
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. The JSON body is nothing but the details of the new user record.
4. Response Validation
Here we usually perform below two types of validation:
1. Validation of Status code
.then().assertThat().statusCode(200);
Since it is a POST request for which we are making a call, thus the expected status code would be 200. Here, we are validating the response based on the condition where if status code from the response matches with the value 200.
2. Validation of Response header
.then().assertThat().header(status, 200);
Here, we are verifying if the server value matches “200” then the assert will return true else false.
Complete Example Code
@Test
public void PostUserDetail() {
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/";
RestAssured.given()
contentType("application/json")
.body(map)
.when()
.post("/user")
.then().assertThat().statusCode(200);
}
Difference between GET and POST request
GET | POST |
In GET, an only a limited amount of data can be sent because data is sent in the header | In POST, a large amount of data can be sent because data is sent in the body |
GET request is not secured because data is exposed in the URL bar | A POST request is secured because data is not exposed in the URL bar |
GET request can be bookmarked and cached | POST request cannot be bookmarked and hardly cached |
GET request is idempotent which means the second request will be ignored until the response of the first request is delivered | A POST request is non-idempotent |
GET request is more efficient and used more than POST | A POST request is less efficient and used less than GET |