如何在 Rest Assured 的 GET 请求体中传递 JSON?

How to Pass JSON in the body of GET Request in Rest Assured?

正在尝试在 get 请求的正文中放心地传递此 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
}

首先你要弄清楚案情。尽管该标准没有明确限制使用 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");

您可以为您的 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" +
            "}";

和rest-assured你可以有这样的东西:

Response response = given()
                .body(Json)
                .when()
                .get("http://restAdress")
                .then()
                .extract().response();

您可以使用 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
}

希望对您有所帮助。