简体   繁体   中英

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

Trying to pass this JSON in the body of a get request in rest assured. Is creating a JSON object a way to go ?

{
    "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
}

You can create a string for your 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" +
            "}";

and with rest-assured you can have something like that :

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

First of all you need to clarify the case. Despite the standard does not put explicit restriction to sending payload with GET request there is a very high probability that your API server or HTTP server that hosts your API code would ignore that payload.

If that is still the case, the simplest way to send your json with RestAssured is:

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");

You can use gson for clean code

Depedency:

implementation 'com.google.code.gson:gson:2.9.0'

Use gson:

import com.google.gson.JsonObject;

Sample code:

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);

Output:

{
    "pharmacyListId": null,
    "pharmacyListName": "PTA",
    "customerSetTypeId": 1,
    "customerId": null,
    "pharmacyId": null,
    "providerAffiliateId": null,
    "providerPlanId": null,
    "providerChain": null
}

I hope this helps.

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