Nagarjuna Reddy Nagarjuna Reddy - 1 month ago 15
reST (reStructuredText) Question

How to pass the request body with multi level data using rest-assured

Trying to send the request body with multi level data using rest-assured in the following way.

Request body:

{
"phoneNumber":1217071016,
"details":
[
{
"id":"123",
"name":"New",
"email":"hello@gmail.com"
},

{
"id":"234",
"name":"next",
"email":"next@gmail.com"
}


]

}



@Test public void generateToken() {

Map<String,String> userDetails = new HashMap<>();

userDetails.put("phoneNumber", "1217071016");
userDetails.put("details.Id", "241342");
userDetails.put("details.name", "New Name");
userDetails.put("details.email", "eclipse@test.com");

Response response = given()

.contentType("application/json")

.queryParam("access_token", "LL6rX8LRP7")

.body(userDetails)

.post("http://site/rest/try/update");


}


When sent in the above way, getting the bad request.

How to pass this kind of data in the above code

Answer

You need not a Map<String, String> but a Map<String, Object>:

Map<String, Object> userDetails = new HashMap<>();
Map<String, Object> details = new HashMap<>();

details.put("id", "241342");
details.put("name", "New Name");
details.put("email", "eclipse@test.com");

userDetails.put("phoneNumber", "1217071016");
userDetails.put("details", Arrays.asList(details, details));