根据以下 JSON 使用 Rest Assured 为 POST 请求传递 JSON 数组

Pass a JSON Array for POST request using Rest Assured based on the following JSON

我是 Rest Assured 的新手,请有人帮我根据以下输出创建正文请求:

{
    "CustomerID": "539177",
    "ReminderTitle": "Demo Reminder Tds",
    "ReminderDescription": "xyz Reminder",
    "ReminderLocation": "New Delhi",
    "ReminderDate": "2020-03-27",
    "ReminderTime": "15:33",
    "attendees": [{
        "CustomerContactID": "122"
    }]
}

示例:

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");
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);

并与 Rest Assured 一起使用

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);