Reputation: 35
I am new to Rest Assured, Please can someone help me to create the body request from the following output:
{
"CustomerID": "539177",
"ReminderTitle": "Demo Reminder Tds",
"ReminderDescription": "xyz Reminder",
"ReminderLocation": "New Delhi",
"ReminderDate": "2020-03-27",
"ReminderTime": "15:33",
"attendees": [{
"CustomerContactID": "122"
}]
}
Example :
Map <String, String> body = new HashMap <String, String> ();
body.put("CustomerID", CustomerID);
body.put("ReminderTitle", "Demo Reminder Tds");
body.put("ReminderDescription", "xyz Reminder");
body.put("ReminderLocation", "New Delhi");
body.put("ReminderDate", "2020-03-27");
body.put("ReminderTime", "15:33");
Upvotes: 0
Views: 5406
Reputation: 803
Rest Assured accepts String objects in .body(String body) method. But for POST and PUT methods only. Check the documentation
Therefore you can just pass the output you received.
String requestBody = "{ \"CustomerID\" : \"539177\", " +
"\"ReminderTitle\" : \"Demo Reminder Tds\", " +
"\"ReminderDescription\" : \"xyz Reminder\", " +
"\"ReminderLocation\" : \"New Delhi\", " +
"\"ReminderDate\" : \"2020-03-27\", " +
"\"ReminderTime\" : \"15:33\", " +
"\"attendees\" : [{\"CustomerContactID\" : \"122\"}] }";
BUT you have to use escape characters in the output String. Then just pass the requestBody;
given()
.body(requestBody)
.when()
.post(URL);
Upvotes: 0
Reputation: 2774
Map<String, Object> map = new LinkedHashMap<>();
map.put("CustomerID", "539177");
map.put("ReminderTitle", "Demo Reminder Tds");
map.put("ReminderDescription", "xyz Reminder");
map.put("ReminderLocation", "New Delhi");
map.put("ReminderDate", "2020-03-27");
map.put("ReminderTime", "15:33");
map.put("attendees", Arrays.asList(new LinkedHashMap<String, Object>() {
{
put("CustomerContactID", "122");
}
}));
Use the below to just print out the output ( you don't have to necessarily )
String abc = new ObjectMapper().writerWithDefaultPrettyPrinter().writeValueAsString(map);
System.out.println(abc);
And to use it with Rest Assured
given().body(map).when().post()
Upvotes: 4