我正在使用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();发布于 2021-02-10 08:31:30
一种简单的方法是使用RestAssured库自动执行Java语言中的应用程序接口响应。查看下面的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请求放在单独的class方法中,并在请求体中返回对象。或者更好的是,您可以探索POJO java类的可能性。
https://stackoverflow.com/questions/62082642
复制相似问题