![](/img/trans.png)
[英]How to send Request as Json Array in body for Post using REST Assured
[英]How to Pass JSON in the body of GET Request in Rest Assured?
尝试在获取请求的正文中传递此 JSON,请放心。 创建 JSON 对象是一种方法吗?
{
"pharmacyListId": null,
"pharmacyListName": "PTA",
"customerSetTypeId": 1,
"customerId": null,
"pharmacyId": null,
"providerAffiliateId": null,
"providerPlanId": null,
"providerChain": null,
"providerNetworkId": null,
"pharmacyZip": null,
"pharmacyZipExtension": null,
"pharmacyStateCode": "MI",
"fillDate": "2021-01-01",
"relationshipId": null,
"organizationId": null,
"paymentCentreId": null,
"providerNpi": null,
"remitReconId": null,
"countryCode": null,
"memberState": null,
"memberZipCode": null,
"memberZipCodeExtension": null
}
您可以为您的 json 创建一个字符串:
String Json = "{\n" +
" \"pharmacyListId\": null,\n" +
" \"pharmacyListName\": \"PTA\",\n" +
" \"customerSetTypeId\": 1,\n" +
" \"customerId\": null,\n" +
" \"pharmacyId\": null,\n" +
" \"providerAffiliateId\": null,\n" +
" \"providerPlanId\": null,\n" +
" \"providerChain\": null,\n" +
" \"providerNetworkId\": null,\n" +
" \"pharmacyZip\": null,\n" +
" \"pharmacyZipExtension\": null,\n" +
" \"pharmacyStateCode\": \"MI\",\n" +
" \"fillDate\": \"2021-01-01\",\n" +
" \"relationshipId\": null,\n" +
" \"organizationId\": null,\n" +
" \"paymentCentreId\": null,\n" +
" \"providerNpi\": null,\n" +
" \"remitReconId\": null,\n" +
" \"countryCode\": null,\n" +
" \"memberState\": null,\n" +
" \"memberZipCode\": null,\n" +
" \"memberZipCodeExtension\": null\n" +
"}";
并且放心,您可以拥有类似的东西:
Response response = given()
.body(Json)
.when()
.get("http://restAdress")
.then()
.extract().response();
首先,您需要澄清案件。 尽管该标准没有明确限制使用GET
请求发送有效负载,但您的 API 服务器或托管 API 代码的 HTTP 服务器很可能会忽略该有效负载。
如果情况仍然如此,使用 RestAssured 发送 json 的最简单方法是:
RestAssured
.with()
.proxy("localhost", proxyServer.getPort())
.body("{\n" +
" \"pharmacyListId\": null,\n" +
" \"pharmacyListName\": \"PTA\",\n" +
" \"customerSetTypeId\": 1,\n" +
" \"customerId\": null,\n" +
" \"pharmacyId\": null,\n" +
" \"providerAffiliateId\": null,\n" +
" \"providerPlanId\": null,\n" +
" \"providerChain\": null,\n" +
" \"providerNetworkId\": null,\n" +
" \"pharmacyZip\": null,\n" +
" \"pharmacyZipExtension\": null,\n" +
" \"pharmacyStateCode\": \"MI\",\n" +
" \"fillDate\": \"2021-01-01\",\n" +
" \"relationshipId\": null,\n" +
" \"organizationId\": null,\n" +
" \"paymentCentreId\": null,\n" +
" \"providerNpi\": null,\n" +
" \"remitReconId\": null,\n" +
" \"countryCode\": null,\n" +
" \"memberState\": null,\n" +
" \"memberZipCode\": null,\n" +
" \"memberZipCodeExtension\": null\n" +
"}\n")
.contentType(ContentType.JSON)
.get("http://YOUR_ENDPOINT");
您可以将gson用于干净的代码
依赖:
implementation 'com.google.code.gson:gson:2.9.0'
使用 gson:
import com.google.gson.JsonObject;
示例代码:
JsonObject body = new JsonObject();
body.addProperty("pharmacyListId", (String) null);
body.addProperty("pharmacyListName", "PTA");
body.addProperty("customerSetTypeId", 1);
body.addProperty("customerId", (String) null);
body.addProperty("pharmacyId", (String) null);
body.addProperty("providerAffiliateId", (String) null);
body.addProperty("providerPlanId", (String) null);
body.addProperty("providerChain", (String) null);
输出:
{
"pharmacyListId": null,
"pharmacyListName": "PTA",
"customerSetTypeId": 1,
"customerId": null,
"pharmacyId": null,
"providerAffiliateId": null,
"providerPlanId": null,
"providerChain": null
}
我希望这有帮助。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.