0

I have nested array in the payload for the POST. Trying to run the POST via rest assured but it throws 400 I tried both hashmap and object mapper but couldn't make it work.

{
    "actions": [
        {
            
            "n": "O",
            "r": "o",
            "value": {
                "value": true
            }
        }
    ],
    "condition": {
        "conditions": [
            {
                
                "i": "o",
                "n": "O",
                "operand": "true",
                "operator": "=",
                "property": "value",
                "rt": "o",
                "type": "boolean"
            }
        ],
        
    },
    "currentStatus": "Enabled",
    
   
}
Kanna
  • 17
  • 4

1 Answers1

0

You can create the request body as follows;

Map<String, Object> requestBody = new HashMap<>();

// Create actions object array
Map<String, Object> valueMap = new HashMap<>();
valueMap.put("value",true);

Map<String, Object> actionsObject = new HashMap<>();
actionsObject.put("n", "O");
actionsObject.put("r", "o");
actionsObject.put("value", valueMap);

Object[] actionsArray = new Object[1];

actionsArray[0] = actionsObject;

requestBody.put("actions", actionsArray);

// Create condition object
Map<String, Object> conditionsObject = new HashMap<>();
conditionsObject.put("i", "o");
conditionsObject.put("n", "O");
conditionsObject.put("operand", "true");
conditionsObject.put("operator", "=");
conditionsObject.put("property", "value");
conditionsObject.put("rt", "o");
conditionsObject.put("type", "boolean");

Object[] conditionsArray = new Object[1];
conditionsArray[0] = conditionsObject;

Map<String, Object> conditionMainObject = new HashMap<>();
conditionMainObject.put("condition", conditionsArray);

requestBody.put("conditions", conditionMainObject);

// add currentStatus
requestBody.put("currentStatus", "Enabled");

JSONObject request = new JSONObject(requestBody);

Then use the request in POST call;

    Response response = RestAssured
            .given(RequestSpec).auth()
            .body(request)
            .post("?id=" + Id);
    int statusCode = response.getStatusCode();
    System.out.println(statusCode);
    assertThat(statusCode, equalTo(200));

Required imports;

import org.json.JSONObject;
import java.util.HashMap;
import java.util.Map;
kaweesha
  • 803
  • 6
  • 16
  • If you find this answer is the correct and acceptable answer for your question, please mark this as the ''accepted answer". [What should I do when someone answers my question?](https://stackoverflow.com/help/someone-answers) – kaweesha Oct 23 '20 at 10:32