英文:
Using queryParams instead of Json body in Restassured API testing
问题
我正在使用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
}
}
而不是使用整个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();
英文:
I am using RestAssured library to automate API responses in Java language and here is the API body I use:
{
"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
}
}
instead of using the whole api body I wanna use queryParameters.
Is there a way doing that?
This is what I have been using so far and keep getting 422 status code error:
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();
答案1
得分: 0
一个简单的方法是使用 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("{" +
"\n" +
"\n" +
"\n" +
"\n" +
"\"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" +
" }" +
"}" +
"}").when().post("api/add/json")
.then().log().all().assertThat().statusCode(200);
}
}
如果你愿意,你可以对响应进行更多的验证,或者你可以使用 JsonPath 类来解析你的 JSON 响应,以便进行进一步的验证。
然而,另一种方法是将 JSON 请求保持在一个单独的类方法中,并在请求体中返回该对象。或者更好的做法是探索使用 POJO Java 类的可能性。
英文:
A simple approach is to using RestAssured library to automate API responses in Java language. See the java class below:
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);
}
}
If you like you can do more validation of the response or you can use the JsonPath class to parse your json responses in a bid to do further validation.
However, another approach is to keep your json request in a separate class' method and returning the object in your request body. Or better still you can explore the possibilities of a POJO java class.
专注分享java语言的经验与见解,让所有开发者获益!
评论