I have written a function to create the body for a POST request sent to a restful webservice as below. The function,refers an array [temp], sends it to a another function which removes null values and return a clean array [tempArray]. Then I create the request body depending on the number of parameters in temp array.
public String createBody()
    throws FileNotFoundException {
    StringBuilder sb = new StringBuilder();
    // function to remove null values and clean array [this has no issues]
    tempArray = cleanArray(temp);
    //check parameter count is valid
    if (tempArray.length % 2 == 0) {
        for (int i = 0; i < tempArray.length; i++) {
            if (i == 0) {
                body = sb.append("{\\").append("\"").append(tempArray[i]).toString();
            }
            //{\"key1\":\"value1\",\"key2\":\"value2\"}"
            else if ((i != 0) && (i != (tempArray.length - 1))) {
                if (i % 2 != 0) {
                    body = sb.append("\\\":\\\"").append(tempArray[i]).toString();
                } else if (i % 2 == 0) {
                    body = sb.append("\\\",\\\"").append(tempArray[i]).toString();
                }
            } else if (i == (tempArray.length - 1)) {
                body = sb.append("\\\":\\\"").append(tempArray[i]).append("\\\"}").toString();
            }
        }
        return body;
    } else {
        return "Invalid";
    } //test this
}
the sample request i make---------------------------------
given().headers("Content-Type", "application/json", "Authorization", "------")
                    .body(createBody()).when().post(BaseUrl)
                    .then()
                            .assertThat().statusCode(200);
The status returned is 400. 
But when I replace body(createBody()) with body({\"email\":\"test@test.com\"}") the request is a success. [When i pass the parameters as a string without passing the return string from the function]. Note that the two strings are identical.
Please help me find me a solution for this.
Thanks in advance..
 
     
     
    