【问题标题】:Using queryParams instead of Json body in Restassured API testing在放心的 API 测试中使用 queryParams 而不是 Json 主体
【发布时间】:2026-01-28 13:25:02
【问题描述】:

我正在使用 RestAssured 库以 Java 语言自动执行 API 响应,这是我使用的 API 主体:

{



"meta": {
    "language": "en",
    "marketCountry": "IN",
    "sourceUrl": "https://cj-gaq-dev.logistics.dhl/regular-shipment.html?en-AU"
},
"contactInformation": {
    "company": "test",
    "firstName": "test",
    "lastName": "test",
    "address": "test",
    "zip": "test",
    "city": "test",
    "country": "IN",
    "countryDisplay": "India",
    "email": "test@test.com",
    "phoneNumber": "2324243243",
    "comments": "test"
},
"shipmentScale": {
    "domestic": false,
    "regional": false,
    "global": true
},
"shipmentProduct": {
    "FREIGHT": {
        "numberOfShipments": "50",
        "frequency": "WEEKLY",
        "checked": true
    }



}

我想使用 queryParameters,而不是使用整个 api 主体。 有没有办法做到这一点?

这是我到目前为止一直在使用的,并且不断收到 422 状态代码错误:

String result = given().header("Content-Type","application/json" )
            .header("Accept","application/json").log().all().queryParam("marketCountry", "IN").queryParam("shipmentScale.domestic", "false")
            .queryParam("shipmentScale.regional", "false").queryParam("shipmentScale.global", "true")
            .queryParam("shipmentProduct.FREIGHT.checked", "true")
            .queryParam("shipmentProduct.FREIGHT.numberOfShipments", "50")
            .queryParam("shipmentProduct.FREIGHT.frequency", "WEEKLY")
            .queryParam("contactInformation.company", "test")
            .queryParam("contactInformation.firstName", "test")
            .queryParam("contactInformation.lastName", "test")
            .queryParam("contactInformation.address", "test")
            .queryParam("contactInformation.zip", "test")
            .queryParam("contactInformation.city", "test")
            .queryParam("contactInformation.email", "test@test.com")
            .queryParam("contactInformation.phoneNumber", "213456")
            .queryParam("contactInformation.comments", "test")
            .queryParam("contactInformation.country", "IN")
            .queryParam("contactInformation.comments", "test")
            .when().post().then().assertThat().statusCode(200).extract().response().asString();

【问题讨论】:

  • 从 REST 的角度来看这没有意义。您可以改用表单参数。
  • 什么是“表单”参数?
  • 这根本没有意义 :) FormParam 和 QueryParam 是资源标识的一部分,为什么要将它作为 URI 的一部分传递而不是作为有效负载传递?
  • 你能提供一个例子吗?
  • “提供一个例子”是什么意思?只需将整个请求包装到 .post() 中即可

标签: java api rest-assured


【解决方案1】:

一种简单的方法是使用 RestAssured 库以 Java 语言自动执行 API 响应。见下面的java类:

public class Basics {

    public static void main(String[] args) {
        
        RestAssured.baseURI = "https://12.23.454.55";
        given().log().all().queryParam("key", "mykey123").header("Content-Type","application/json")
        .body("{\r\n" + 
                "\r\n" + 
                "\r\n" + 
                "\r\n" + 
                "\"meta\": {\r\n" + 
                "    \"language\": \"en\",\r\n" + 
                "    \"marketCountry\": \"IN\",\r\n" + 
                "    \"sourceUrl\": \"https://cj-gaq-dev.logistics.dhl/regular-shipment.html?en-AU\"\r\n" + 
                "},\r\n" + 
                "\"contactInformation\": {\r\n" + 
                "    \"company\": \"test\",\r\n" + 
                "    \"firstName\": \"test\",\r\n" + 
                "    \"lastName\": \"test\",\r\n" + 
                "    \"address\": \"test\",\r\n" + 
                "    \"zip\": \"test\",\r\n" + 
                "    \"city\": \"test\",\r\n" + 
                "    \"country\": \"IN\",\r\n" + 
                "    \"countryDisplay\": \"India\",\r\n" + 
                "    \"email\": \"test@test.com\",\r\n" + 
                "    \"phoneNumber\": \"2324243243\",\r\n" + 
                "    \"comments\": \"test\"\r\n" + 
                "},\r\n" + 
                "\"shipmentScale\": {\r\n" + 
                "    \"domestic\": false,\r\n" + 
                "    \"regional\": false,\r\n" + 
                "    \"global\": true\r\n" + 
                "},\r\n" + 
                "\"shipmentProduct\": {\r\n" + 
                "    \"FREIGHT\": {\r\n" + 
                "        \"numberOfShipments\": \"50\",\r\n" + 
                "        \"frequency\": \"WEEKLY\",\r\n" + 
                "        \"checked\": true\r\n" + 
                "    }\r\n" + 
                "\r\n" + 
                "\r\n" + 
                "\r\n" + 
                "}").when().post("api/add/json")
        .then().log().all().assertThat().statusCode(200);

    }

}

如果您愿意,您可以对响应进行更多验证,或者您可以使用 JsonPath 类来解析您的 json 响应以进行进一步验证。

但是,另一种方法是将 json 请求保存在单独的类方法中,并在请求正文中返回对象。或者更好的是,您可以探索 POJO java 类的可能性。

【讨论】:

    最近更新 更多