How do i send JsonObject with nested values as Post request in REST assured

user3431212 picture user3431212 · Mar 18, 2014 · Viewed 15.9k times · Source

I am using rest assured -https://code.google.com/p/rest-assured/wiki/Usage My JsonObject looks like this

{
"id": "12",
"employeeInfo": null,
"employerInfo": null,
"checkDate": 1395093997218,
"netAmount": {
"amount": 70,
"currency": "USD"
},
"moneyDistributionLineItems": [
{
"mAmount": 100,
"employeeBankAccountId": "BankAccount 1"
}
],
}

how can i send this as part of parameters using REST-assured POST? I have tried

given().param("key1", "value1").param("key2", "value2").when().post("/somewhere").then().
        body(containsString("OK")); 

but that is not scalable for HUGE objects with nested values. Is there a better approach?

Answer

Johan picture Johan · Mar 18, 2014

You just send the JSON document in the body. For example if you have your JSON document in a String called myJson then you can just do like this:

String myJson = ..
given().contentType(JSON).body(myJson).when().post("/somewhere"). .. 

You can also use a POJO, input stream and byte[] instead of a String.