【问题标题】:Pass a JSON Array for POST request using Rest Assured based on the following JSON使用基于以下 JSON 的 Rest Assured 为 POST 请求传递 JSON 数组
【发布时间】:2020-05-11 13:01:37
【问题描述】:

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

【问题讨论】:

    标签: rest-assured


    【解决方案1】:
    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()
    

    【讨论】:

    • 我的 CustomerID 和 CustomerContactID 以字符串形式出现,因此无法正确传递。显示 {"status":"0","message":"Invalid format for : CustomerContactID"} String customerIDValue = PropFileHandler.readProperty("CustomerID"); HeaderConfigs header = new HeaderConfigs(); PostAPIBuilders builder = new PostAPIBuilders();响应响应 = RestAssured.given().headers(header.defaultHeaders()).body(builder.postCustomerReminder(customerIDValue, CustomerContactID)) .when().post(APIPath.apiPath.CUSTOMERREMINDERAPI);
    • 如果您需要它们作为 int,那么只需删除引号,map.put("CustomerID", 539177)put("CustomerContactID", 122);您提供的示例 JSON 将这些值作为字符串,所以我按原样构建它
    • 我将它作为 string.public Map postCustomerReminder(String CustomerID, final String map2){ Map map = new LinkedHashMap(); map.put("CustomerID", CustomerID); map.put("ReminderTitle", "Demo Reminder Tds"); map.put("ReminderDescription", "xyz Reminder"); map.put("提醒位置", "新德里"); map.put("提醒日期", "2020-03-27"); map.put("提醒时间", "15:33"); map.put("参加者", Arrays.asList(new LinkedHashMap() { { put("CustomerContactID", map2); } }));返回地图; }
    • 对我有用的是:JSONObject map = new JSONObject(); map.put("CustomerID", CustomerID); map.put("ReminderTitle", "Demo Reminder Tds"); map.put("ReminderDescription", "xyz Reminder"); map.put("提醒位置", "新德里"); map.put("提醒日期", "2020-03-27"); map.put("提醒时间", "15:33"); map.put("参加者", Arrays.asList(new LinkedHashMap() { { put("CustomerContactID", CustomerContactID); } }));返回地图;
    【解决方案2】:

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

    【讨论】:

      猜你喜欢
      • 2021-02-14
      • 2021-08-23
      • 1970-01-01
      • 2020-06-23
      • 1970-01-01
      • 2021-02-07
      • 2016-12-03
      • 1970-01-01
      • 2019-02-18
      相关资源
      最近更新 更多