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

Time:04-27

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
}

CodePudding user response:

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

CodePudding user 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");
  • Related