Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JSON POST using RestAssured

I am trying to create a test to validate the response of a JSON Post is as expected.

I am trying to test the POST of a JSON message body to a URL which is turn then sends a text message and if successful it sends a response that it was successful again in JSON format.

My test is as follows

public void simpleTest() {

      String myJson = "{\"phoneNumber\":\"353837986524\", \"messageContent\":\"test\"}";
      given()
              .port(31111) // port number
              .header("Content-Type", "application/json")
              .body(myJson)
              .when()
              .post("/testenvironment/text/send")
              .then().assertThat()
              .body("message", equalTo("{\"resultMessage\":\"Message accepted\"}"));
  }

But seem to be getting this exception

java.lang.IllegalStateException: You can either send form parameters OR body content in POST, not both!

And I'm not sure what the issue is?

like image 752
user3520080 Avatar asked Jan 19 '16 16:01

user3520080


People also ask

How do I send a payload in a POST request?

Sending a payload post("https://restful-booker.herokuapp.com/auth"); String authResponse = response. getBody(). print(); assertThat(authResponse, containsString("token")); So we begin by calling AuthPayload to create a new Java Object with the values we want to send in the HTTP POST request.

What is JSONPath in Restassured?

JsonPath is an alternative to using XPath for easily getting values from a Object document. It follows the Groovy dot notation syntax when getting an object from the document. You can regard it as an alternative to XPath for XML.


2 Answers

Restassured is failing to parse Json as per the stack trace. I use org.json jar, which is more elegant way to handle big json inputs. There are other implementations of json handling in java, which can be used based on your preference.

Coming to your code:

public void simpleTest() {

   // use org.json JSONObject to define your json
   JSONObject jsonObj = new JSONObject()
                             .put("phoneNumber","353837986524")
                             .put("messageContent","test");

   given()
      .port(31111) // port number
      .contentType("application/json")  //another way to specify content type
      .body(jsonObj.toString())   // use jsonObj toString method
   .when()
      .post("/testenvironment/text/send")
   .then()
      .assertThat()
      .body("message", equalTo("{\"resultMessage\":\"Message accepted\"}"));
}

Also, I didnt find what the output of the rest service in the question. For example it is returning a a json {"resultMessage":"Message accepted"} you should be validating the response in the following way:

...
.body("resultMessage",equalTo("Message accepted"));
like image 120
parishodak Avatar answered Oct 07 '22 16:10

parishodak


Try changing the mimeType to a header instead of a parameter.

And based on the information you shared I think what you need is the Content-Type header, not mimeType.

like image 44
Bathiya Priyadarshana Avatar answered Oct 07 '22 16:10

Bathiya Priyadarshana