How to Create an request in restAssured [Complete understanding from create request to parse json with mentioned few lines]:

How to Create an request in restAssured [Complete understanding from create request to parse json with mentioned few lines]:


public void createRestRequest() throws ParseException, JsonParseException, JsonMappingException, IOException {

RestAssured.baseURI="";

RequestSpecification req=RestAssured.given();

//Add Authentication in rest assured API

req.auth().basic("Base64 encoded usernam", "");

//set header

req.header("Content-Type", "application/json");

//set param

req.pathParam("", "");

//mset multiple param

req.pathParams("", "", "");

//Set Query param

req.param("", "");

req.queryParams("", "", "");

//Set the cookies

Cookie co=new Cookie.Builder("session_id", "1234")

.setSecured(true)

.setComment("")

.build();

req.cookie(co);

req.body("");

Response resp=req.post("/test");

//3 ways to convert string to JSON

//1- JSON Object using Gson

//Gson is google laborary

Gson g=new Gson();

WFResponse res=g.fromJson("", WFResponse.class);

//java object to json

String str =g.toJson("");

//2- Json String to java object using JSON

JSONParser parser=new JSONParser();

JSONObject json=(JSONObject)parser.parse("");

WFResponse json1=(WFResponse)parser.parse("");

//3 String to JOSN 

WFResponse res2=new ObjectMapper().readValue("",WFResponse.class);

}


To view or add a comment, sign in

Others also viewed

Explore topics