简体   繁体   中英

How to post values from RestAssured to JSon and get a value from it?

{"service_request":  {"service_type":"Registration","partner":"1010101","validity":1,"validity_unit":"Year","quantity":1,"center":"1020301","template":"2020301"}}

I have JSON values as above obtained from Request Payload, when I post data from the web page. I want to post this data from RESTAssured in java and want to get the returned value? How can I do that?

I would recommend to check https://github.com/jayway/rest-assured/wiki/Usage#specifying-request-data

I imagine you are trying to do a POST using Rest-Assured.For that simplest and easy way is.. Save your payload as String. If you have payload as JSONObject, use toJSONString() to convert it to String

String payload = "yourpayload";
String responseBody = given().contentType(ContentType.JSON).body(payload).
when().post(url).then().assertThat().statusCode(200).and()
extract().response().asString();

Above code, post your payload to given Url, verify response code and convert response body to a String. You can do assertion without converting to String as well like

given().contentType(ContentType.JSON).body(payload).
when().post(url).then().assertThat().statusCode(200).
and().assertThat().body("service_request.partner", equalTo("1010101"));

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM