8

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?

bjhaid
  • 9,592
  • 2
  • 37
  • 47
user3431212
  • 83
  • 1
  • 1
  • 3

2 Answers2

9

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.

Johan
  • 37,479
  • 32
  • 149
  • 237
  • 1
    so there is no way to pass nested valued in REST Assured ? I want to do parameterization and if we use JSON static file then it is hard, or other way is to create JSON file in run time for that particular request/API and use it.. :( – Paresh Apr 12 '15 at 00:27
  • oh I think, even with the above mentioned method we can parameterized the sample!! Thx – Paresh Apr 12 '15 at 01:22
  • You can parameterize it in other ways, for example using a template engine such as jmte (https://code.google.com/p/jmte/). – Johan Apr 12 '15 at 04:23
2
    URL file = Resources.getResource("PublishFlag_False_Req.json");
    String myJson = Resources.toString(file, Charsets.UTF_8);

    Response responsedata = given().header("Authorization", AuthorizationValue)
        .header("X-App-Client-Id", XappClintIDvalue)
        .contentType("application/vnd.api+json")
        .body(myJson)
        .with()
        .when()
        .post(dataPostUrl);
mszymborski
  • 1,615
  • 1
  • 20
  • 28
Aminul
  • 21
  • 1