1

Below is the code I am using

JSONObject requestParams = new JSONObject();
requestParams.put("something", "something value");
requestParams.put("another.child", "child value");

This is how the api needs to be posted

{

   "something":"something value",
   "another": {
   "child": "child value"
   }
}

I get an error stating that the

The another.child field is required.

How do I go about posting this via restAssured? The other APIs do not require posting with nesting work so I'm assuming that's why it's failing.

Saeid Amini
  • 1,313
  • 5
  • 16
  • 26
neha gupta
  • 11
  • 1
  • 2

1 Answers1

1

Consider another as Json object within the main Json object.

JSONObject requestParams = new JSONObject();
requestParams.put("something", "something value");

JSONObject anotherObject = new JSONObject();
anotherObject.put("child", "child value");

requestParams.put("another", anotherObject);

System.out.println(requestParams.toString());

This will print;

{"another":{"child":"child value"},"something":"something value"}

To pass this Json object to Rest Assured POST method;

    RestAssured.baseURI="base_url";

    RestAssured.given()
            .when()
            .post(requestParams.toString());

Required imports;

import org.json.JSONObject;
import io.restassured.RestAssured;
kaweesha
  • 803
  • 6
  • 16
  • Thank you . I had to send something like changeDetails" : { "requirement" : "https://jira" } So , I used changeDetailsObject.put("requirement", "https://jira"); jsonPayloadBody.put("changeDetails", changeDetailsObject); . I upvoted this answer and question. – Sameera De Silva Mar 01 '22 at 07:38