【发布时间】: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