Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to write array request body in rest assured

I have used rest assured framework, This is the request body for post method

{
  "requestNumber":749 ,
  "referenceNumber": "tyryrty",
  "cardType": "aliqua pariatur enim cupidatat",
  "companyName": "amet Lorem",
  "rejectMessage": "tempor dolor officia",
  "contactEmail": "mollit pariatur veniam sed",
  "serviceProviderEmailList": [
    "ipsum nis",
    "Ut dolore aliqua exercitation irure"
  ],
  "approvedBy": "sup",
  "approvedDate": "1962-07-15T01:00:55.437Z"
}

I have tried in this way but i’m not getting result

 
JSONObject jsonobj1 = new JSONObject();
            jsonobj1.put("requestNumber", 749 );
            jsonobj1.put("referenceNumber", "tyryrty");
            jsonobj1.put("cardType", "aliqua pariatur enim cupidatat");
            jsonobj1.put("companyName", "amet Lorem");
            jsonobj1.put("contactEmail", "tempor dolor officia");
            Map<String, Object> map= new HashMap<String,Object>();
            map.put("serviceProviderEmailList", "ipsum nis");
            List<Map<String, Object>> test=Arrays.asList(map);
            jsonobj1.put("serviceProviderEmailList", test);
            
            jsonobj1.put("approvedBy", "sup");
            jsonobj1.put("approvedDate", "1962-07-15T01:00:55.437Z");

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

serviceProviderEmailList field is an array of strings, but you are trying to add a single string value to it using a map. Instead, you should create a list of strings and add it to the JSON object as follows:

 List<String> serviceProviderEmailList = Arrays.asList("ipsum nis", "Ut dolore aliqua exercitation irure");
jsonobj1.put("serviceProviderEmailList", serviceProviderEmailList);
jsonobj1.put("rejectMessage", "tempor dolor officia");

you can use the body method of the RequestSpecification class to set the request body of a POST request, as follows:

given().contentType("application/json")
       .body(jsonobj1.toString())
       .when().post("http://your-api-endpoint.com/")
       .then().statusCode(200);
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading