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();
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.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.