如何使用嵌套值发送JsonObject作为REST保证的Post请求

问题描述 投票:8回答:2

我正在使用放心-https://code.google.com/p/rest-assured/wiki/Usage我的JsonObject看起来像这样

{
"id": "12",
"employeeInfo": null,
"employerInfo": null,
"checkDate": 1395093997218,
"netAmount": {
"amount": 70,
"currency": "USD"
},
"moneyDistributionLineItems": [
{
"mAmount": 100,
"employeeBankAccountId": "BankAccount 1"
}
],
}

如何使用REST保证的POST将其作为参数的一部分发送?我试过了

given().param("key1", "value1").param("key2", "value2").when().post("/somewhere").then().
        body(containsString("OK")); 

但对于具有嵌套值的HUGE对象,这是不可扩展的。有更好的方法吗?

http-post jsonobject rest-assured
2个回答
8
投票

您只需在正文中发送JSON文档。例如,如果你在名为myJson的String中有你的JSON文档,那么你可以这样做:

String myJson = ..
given().contentType(JSON).body(myJson).when().post("/somewhere"). .. 

您还可以使用POJO,输入流和byte []而不是String。


2
投票
    URL file = Resources.getResource("PublishFlag_False_Req.json");
    String myJson = Resources.toString(file, Charsets.UTF_8);

    Response responsedata = given().header("Authorization", AuthorizationValue)
        .header("X-App-Client-Id", XappClintIDvalue)
        .contentType("application/vnd.api+json")
        .body(myJson)
        .with()
        .when()
        .post(dataPostUrl);
© www.soinside.com 2019 - 2024. All rights reserved.