如何使用Java restassured库发布参数?

时间:2016-11-23 15:09:09

标签: java post rest-assured

我想使用查询参数发送帖子请求,如

https://10.40.0.20:8443/randomText/randomText/randomText?registrationInfo=%7B%22publicIpAddress%22%20%3A%20%22193.168.0.1%22%2C%20%22networkIpAddress%22%20%3A%20%22192.168.0.1%22%2C%20%22numberOfPartitions%22%20%3A%202%2C%20%22softwareVersion%22%20%3A%20%22v123%22%2C%20%22publicSshKey%22%20%3A%20%22ssh-key%22%2C%20%22macAddress%22%20%3A%20%2226%3A18%3A31%3Abf%3A19%3Aea%22%20%7D

如何使用Java的restassured库?

given().contentType(ContentType.JSON)
                .header("accept", "application/json")
                .header("Content-Type", "application/application.json")
                .parameters("publicIpAddress", "194.168.0.1", "networkIpAddress",
                        "192.168.0.1", "numberPartitions",
                        1, "softwareVersion", "v123",
                        "publicSshKey", "ssh-key", "macAddress",
                        "1a:57:39:96:30:10")
                .log().method()
                .log().path()
                .expect()
                .statusCode(201)
                .when()
                .post("/uri/")
                .then()
                .log().status()
                .log().body();

这是我到目前为止所写的内容,但它不起作用。它向我发送了400响应代码,语法错误。

我也使用了以下内容,但对我不起作用:

given().contentType(ContentType.JSON)
                .queryParam("publicIpAddress", "194.168.0.1")
                .queryParam("commsIpAddress", "192.168.0.1")
                .queryParam("numberOfActivePartitions", 1)
                .queryParam("testAgentSoftwareVersion", "v123")
                .queryParam("publicSshKey", "ssh-key")
                .queryParam("commsMac", "1a:57:39:96:30:10")
                .header("accept", "application/vnd.cobham.v1+json")
                .log().method()
                .log().path()
                .expect()
                .statusCode(201)
                .when()
                .post("/controllers/")
                .then()
                .log().status()
                .log().body();

1 个答案:

答案 0 :(得分:0)

您应该使用queryParam

given().queryParam("name, "value")
                 .body(..)
                 .when()
                 .post(..);