11

I am using rest assured for the post request containing the JSON body

My post request code is :-

RestAssuredResponseImpl stat=
            (RestAssuredResponseImpl)given().
            header("Accept", "application/json").
            header("Content-Type", "application/json").
            header("userid", "131987”).
            queryParam("name", "Test12").
            queryParam("title", "Test127123").
            queryParam("contactEmail", “abc@gmail.com").
            queryParam("description", "testing purpose").
            when().post("").thenReturn().getBody();

I am getting the following error:-

{"errors":{"error":{"code":400,"type":"HttpMessageNotReadableException","message":"Required request body content is missing: org.springframework.web.method.HandlerMethod$HandlerMethodParameter@8e9299c"}}}

Kindly help....

dogant
  • 1,376
  • 1
  • 10
  • 23

4 Answers4

12

Looks like your server is expecting a request body but you're sending the data as query parameters. If I understand it correctly you want to send your data as JSON. The easiest way to do this is using this approach:

Map<String, Object>  map = new HashMap<>();
map.put("name", "Test12");
map.put("title", "Test127123");
map.put("contactEmail", "abc@gmail.com");
map.put("description", "testing purpose");

ResponseBody = 
given().
        accept(ContentType.JSON).
        contentType(ContentType.JSON).
        header("userid", "131987").
        body(map).
when().
        post("").
thenReturn().body();
Sandeep
  • 455
  • 4
  • 26
Johan
  • 37,479
  • 32
  • 149
  • 237
1

Check the fully qualified path for POST URI. The body for the post call is missing, which is mandatory. ur call should be like:

Response _res = requestspec.body(jsonObject).post(url);

Then you can perform operations on Response.

1

//Try This this code, it helped ME may help You as well.

    {
RestAssured.baseURI = API_URL;
RequestSpecification request = RestAssured.given();
request.header("Key1", "Value1");
request.header("Key2", ""+Value2+""); //If value is getting capture from other variable
JSONObject requestParams = new JSONObject();
requestParams.put("Payload Key1", "Payload Value1"); 
requestParams.put("Payload Key2", "Payload Value2");
request.body(requestParams.toString());
Response response = request.post(""); 
int StatusCode = response.getStatusCode(); 
System.out.println("Status code : " + StatusCode);       
System.out.println("Response body: " + response.body().asString()); //Get Response Body
}
AnkitSingh
  • 23
  • 4
0

queryParam is used when we have to pass the query parameter in the URL.In body section we have to send actual content.

You can send content in the following ways:

### 1.Passing JSON data format in the body:-
    given() 
    .accept(ContentType.JSON) 
    .contentType(ContentType.JSON) 
    .body("{\r\n" + "    \"id\": 101,\r\n" +
    "    \"firstName\": \"john\",\r\n" + "    \"lastName\": \"doe\",\r\n" +
    "    \"email\": \"loahsda@gmail.com\",\r\n" +
    "    \"mobile\": \"981-232-3435\",\r\n" + "    \"dateOfBirth\": 78468436\r\n"
    + "}" )
    .post(URL)
    .then() 
    .statusCode(200).log().all() 

### Second Way
    JSONObject json1 = new JSONObject(); 
    json1.put("firstName","John");
    json1.put("lastName","Joker"); 
    json1.put("email","loahsda@gmail.com");
    json1.put("mobile","981-232-3435"); 
    json1.put("dateOfBirth",78468436);
    Response response1 = 
    given()
    .pathParam("name", "object1")
    .accept(ContentType.JSON) 
    .contentType(ContentType.JSON)
    .body(json1.toString())
    .post("https://xxx/{name}") 
    .then()
    .statusCode(200)
    .log().all();
slfan
  • 8,950
  • 115
  • 65
  • 78
Shivani
  • 51
  • 1
  • 9