我是新来的放心人。请有人帮我从以下输出创建正文请求:
{“ CustomerID”:“ 539177”,“ ReminderTitle”:“ Demo Reminder Tds”,“ ReminderDescription”:“ xyz提醒”,“ ReminderLocation”:“新德里”,“ ReminderDate”:“ 2020-03-27”,“ ReminderTime”:“ 15:33”,“与会者”:[{“ CustomerContactID”:“ 122”}]}
示例:地图正文= new HashMap();
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");
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");
}
}));
使用以下内容仅打印输出(不必一定要使用)
String abc = new ObjectMapper().writerWithDefaultPrettyPrinter().writeValueAsString(map);
System.out.println(abc);
并与“放心的人”一起使用
given().body(map).when().post()
Rest Assured接受.body(String body)方法中的String对象。但仅适用于POST和PUT方法。检查documentation
因此,您可以只传递您收到的输出。
String requestBody = "{ \"CustomerID\" : \"539177\", " +
"\"ReminderTitle\" : \"Demo Reminder Tds\", " +
"\"ReminderDescription\" : \"xyz Reminder\", " +
"\"ReminderLocation\" : \"New Delhi\", " +
"\"ReminderDate\" : \"2020-03-27\", " +
"\"ReminderTime\" : \"15:33\", " +
"\"attendees\" : [{\"CustomerContactID\" : \"122\"}] }";
但是您必须在输出字符串中使用转义字符。然后只需传递requestBody;
given()
.body(requestBody)
.when()
.post(URL);