'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
}


Solution 1:[1]

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

Solution 2:[2]

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

Solution 3:[3]

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.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 raskoo
Solution 2 Alexey R.
Solution 3 Fransiskus Andika Setiawan