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?

2
  • mention what you tried? Commented Mar 18, 2014 at 2:28
  • yes there is a better approach, use Karate instead of REST-assured: github.com/intuit/karate Commented Dec 24, 2017 at 5:09

2 Answers 2

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.

Sign up to request clarification or add additional context in comments.

3 Comments

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.. :(
oh I think, even with the above mentioned method we can parameterized the sample!! Thx
You can parameterize it in other ways, for example using a template engine such as jmte (code.google.com/p/jmte).
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);

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.