使用Rest Assured DSL执行发布请求

时间:2015-08-18 13:50:28

标签: rest-assured

这是我的帖子请求的正文

given().
                param("type", "Something").
                param("key1", "value1").
                param("key2", "value2").
                param("key3", "value3").
                header("content-type", "application/json").
                when().
                    post("http://someURL/something").
                then().
                statusCode(200).
                log().everything();

我不太确定如何模拟发送上述后期有效负载的发布请求的参数。

我假设将所有内容作为键值对发送,但没有考虑作为数组的身份验证中的嵌套。除外,我得到400 Bad Request。

我很高兴了解如何为此请求正确发送帖子参数。除了可读性之外,在Map中发送它是否有任何区别

这是我的RestAssured DSL

IDLE

2 个答案:

答案 0 :(得分:1)

只需创建一个这样的哈希映射:

Map<String, Object> map = new HashMap<>();
map.put("Type", "Something");
map.put("Authentication", asList(new HashMap<String, Object>() {{
    put("Key", "key1");
    put("Value", "value1");
}}, new HashMap<String, Object>() {{
    put("Key", "key2");
    put("Value", "value2");
}}, new HashMap<String, Object>() {{
    put("Key", "key3");
    put("Value", "value3");
}}));

并将其传递给REST Assured的主体:

given().               
        contentType(ContentType.JSON).
        body(map).
when().
        post("http://someURL/something").
then().
        statusCode(200).
        log().everything();

如果您愿意,也可以创建POJO而不是地图并将其传递给正文。为此,您需要在类路径中拥有受支持的JSON序列化程序框架。例如jackson-databind。有关详细信息,请参阅documentation

答案 1 :(得分:0)

    String inputPayLaod = "{
    "Type": "Something",
    "Authentication": [
        {
            "Key": "key1",
            "Value": "value1"
        },
        {
            "Key": "key2",
            "Value": "value2"
        },
        {
            "Key": "key3",
            "Value": "value3"
        }
    ]
}";

given().contentType(ContentType.JSON)
.body(inputPayLoad)
.when()
.post(url)
.then().statusCode(200);
相关问题