Search code examples
restrest-assuredrest-assured-jsonpath

API Orchestration in rest assured during run time


How to build a post request after taking some values from other post response on the run time in rest assured.

Suppose I have a big Json file which I am going to send it as request modifying just 2 or 3 key's values taking that value from another response.


Solution

  • Pre-requisite: Add the below dependency in your Maven.

    <dependency>
            <groupId>org.json</groupId>
            <artifactId>json</artifactId>
            <version>20180130</version>
    </dependency>
    

    Say you have a post response body in the string format response.getBody().asString() from the first request as below,

    {
    "id": 13245,
    "name": "firstName",
    "phone": 1234567890
    }
    

    Convert the above string to a JSONObject and manipulate data as below

        JSONObject jsonObject = new JSONObject(response.getBody().asString());
    
        jsonObject.put("id", 54321);
    
        jsonObject.put("name", "lastName");
    
        System.out.println(jsonObject.toString());
    

    Your output will print as following

    {"phone":1234567890,"name":"lastName","id":54321}