0

I am new to Rest Assured, Please can someone help me to create the body request from the following output:

{
    "CustomerID": "539177",
    "ReminderTitle": "Demo Reminder Tds",
    "ReminderDescription": "xyz Reminder",
    "ReminderLocation": "New Delhi",
    "ReminderDate": "2020-03-27",
    "ReminderTime": "15:33",
    "attendees": [{
        "CustomerContactID": "122"
    }]
}

Example :

Map <String, String> body = new HashMap <String, String> ();

body.put("CustomerID", CustomerID);
body.put("ReminderTitle", "Demo Reminder Tds");
body.put("ReminderDescription", "xyz Reminder");
body.put("ReminderLocation", "New Delhi");
body.put("ReminderDate", "2020-03-27");
body.put("ReminderTime", "15:33");

2 Answers 2

4
Map<String, Object> map = new LinkedHashMap<>();
map.put("CustomerID", "539177");
map.put("ReminderTitle", "Demo Reminder Tds");
map.put("ReminderDescription", "xyz Reminder");
map.put("ReminderLocation", "New Delhi");
map.put("ReminderDate", "2020-03-27");
map.put("ReminderTime", "15:33");
map.put("attendees", Arrays.asList(new LinkedHashMap<String, Object>() {
    {
        put("CustomerContactID", "122");
    }
}));

Use the below to just print out the output ( you don't have to necessarily )

String abc = new ObjectMapper().writerWithDefaultPrettyPrinter().writeValueAsString(map);
System.out.println(abc);

And to use it with Rest Assured

given().body(map).when().post()
Sign up to request clarification or add additional context in comments.

4 Comments

My CustomerID and CustomerContactID are coming as string so it is not able to pass it correctly. Showing {"status":"0","message":"Invalid format for : CustomerContactID"} String customerIDValue = PropFileHandler.readProperty("CustomerID"); HeaderConfigs header = new HeaderConfigs(); PostAPIBuilders builder = new PostAPIBuilders(); Response response = RestAssured.given().headers(header.defaultHeaders()).body(builder.postCustomerReminder(customerIDValue, CustomerContactID)) .when().post(APIPath.apiPath.CUSTOMERREMINDERAPI);
If you need them as int then just remove the quotes, map.put("CustomerID", 539177) and put("CustomerContactID", 122);The sample JSON you provided had those values as strings so I framed it as is
I am passing it as string.public Map<String, Object> postCustomerReminder(String CustomerID, final String map2){ Map<String, Object> map = new LinkedHashMap<String, Object>(); map.put("CustomerID", CustomerID); map.put("ReminderTitle", "Demo Reminder Tds"); map.put("ReminderDescription", "xyz Reminder"); map.put("ReminderLocation", "New Delhi"); map.put("ReminderDate", "2020-03-27"); map.put("ReminderTime", "15:33"); map.put("attendees", Arrays.asList(new LinkedHashMap<String, Object>() { { put("CustomerContactID", map2); } })); return map; }
What worked for me as well was: JSONObject map = new JSONObject(); map.put("CustomerID", CustomerID); map.put("ReminderTitle", "Demo Reminder Tds"); map.put("ReminderDescription", "xyz Reminder"); map.put("ReminderLocation", "New Delhi"); map.put("ReminderDate", "2020-03-27"); map.put("ReminderTime", "15:33"); map.put("attendees", Arrays.asList(new LinkedHashMap<String, Object>() { { put("CustomerContactID", CustomerContactID); } })); return map;
0

Rest Assured accepts String objects in .body(String body) method. But for POST and PUT methods only. Check the documentation

Therefore you can just pass the output you received.

        String requestBody = "{ \"CustomerID\" : \"539177\", " +
            "\"ReminderTitle\" : \"Demo Reminder Tds\", " +
            "\"ReminderDescription\" : \"xyz Reminder\", " +
            "\"ReminderLocation\" : \"New Delhi\", " +
            "\"ReminderDate\" : \"2020-03-27\", " +
            "\"ReminderTime\" : \"15:33\", " +
            "\"attendees\" : [{\"CustomerContactID\" : \"122\"}] }";

BUT you have to use escape characters in the output String. Then just pass the requestBody;

    given()
        .body(requestBody)
        .when()
        .post(URL);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.