Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 556
user3431212 Avatar asked Mar 18 '14 02:03

user3431212


2 Answers

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.

like image 122
Johan Avatar answered Oct 26 '22 15:10

Johan


    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);
like image 2
Aminul Avatar answered Oct 26 '22 16:10

Aminul