Pages

Tuesday, 5 May 2020

Rest API Post method automation in java with rest Assured ? API automation Interview Question-SDET

       

  











Automate a POST method with REST ASSURED  in 3 different ways

Input Request Body for POST method:
Input format:
=====
{
"FirstName" : "test",

"LastName" : "user",

"UserName" : "userone",

"Password" : "pass@word"

}
=========
Three ways to pass the request body as json for POST method

1.Pass it directly in a string

Change your json body to escaped string using online converter:
"https://www.freeformatter.com/json-escape.html#ad-output"

        //pass the escaped string in a string variable

             String jsonBody = "{\r\n\"FirstName\" : \"trsrsrstsdfadf\",\r\n\r\n\"LastName\" : \"sfsgfsgfs\",\r\n\r\n\"UserName\" : \"hgshgshgs\",\r\n\r\n\"Password\" : \"ytytytytyttyty\"\r\n\r\n}";

            Response resp = given().header("Content-Type", "application/json")
                    .body(jsonBody)
                    .when().post("URL");
       
//validate the status code of the response
                assertEquals(resp.getStatusCode(),200);

2.Create jsonobject


pass all the parameter in put method for the object created class JsonObject

//create a object for class jsonobject and add all the json key value into hashmap with put method

JSONObject requestParams = new JSONObject();
            requestParams.put("key1", "trsrsrstsdfadf");
            requestParams.put("key2", "sfsgfsgfs");
            requestParams.put("key3", "hgshgshgs");
            requestParams.put("key4", "ytytytytyttyty");

           //pass the object as input after converting it to string
         
            Response resp = given().header("Content-Type","application/json").
                        body(requestParams.toString()). 
                when().
                        post("URL");
       
               //validate status code

                assertEquals(resp.getStatusCode(),200);

3. Create a json file, parse it and send it as request body

Copy paste your request body in a json file and save it with .json extension: for eg: testdataPost.json

//Fetch the json file which contains the request body in json format
//System.getProperty("user.dir")+"\\TestData\\testdataPost.json"==> path where we keep our json with request body

FileInputStream file = new FileInputStream(new File(System.getProperty("user.dir")+"\\TestData\\testdataPost.json"));

//use IOUtils to convert your input stream into string

            Response resp = given().header("Content-Type", "application/json")
                    .body(IOUtils.toString(file,"UTF-8"))
                    .when().post("pass URL");
               
                assertEquals(resp.getStatusCode(),200);

***********************************
Note: If anyone need help in designing framework or virtual support for Automation work then reach out to me @sidharth.shukla19@gmail.com/9619094122
***********************************
Check below link on top Interview preparation:RestAPI

SQL Query   Cucumber-BDD    Linux Commands    Maven   GIT 

TestNG@after@before
 

*************************************************

API Testing Interview Question Set:
https://automationreinvented.blogspot.com/search/label/Rest-API

 
Kubernetes Interview Question Set
https://automationreinvented.blogspot.com/search/label/Kubernetes

 
Docker Interview Question Set
https://automationreinvented.blogspot.com/2020/02/top-18-docker-commands-for-aytomation.html

 
Linux Interview question Set
https://automationreinvented.blogspot.com/search/label/Linux

 
Automation Testing/SDET Framework Design
https://automationreinvented.blogspot.com/search/label/FrameworkDesign


Java Related Interview Question Set

https://automationreinvented.blogspot.com/search/label/Java


GIT Interview Question Set:
https://automationreinvented.blogspot.com/search/label/GIT


Coding Interview Question Set:
https://automationreinvented.blogspot.com/search/label/Coding%20Questions



No comments:

Post a Comment