POST request in rest assured

Akshay Rajesh Dwivedi picture Akshay Rajesh Dwivedi · Aug 7, 2015 · Viewed 24.2k times · Source

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", “[email protected]").
            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....

Answer

Johan picture Johan · Aug 15, 2015

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>  jsonAsMap = new HashMap<>();
map.put("name", "Test12");
map.put("title", "Test127123");
map.put("contactEmail", “[email protected]");
map.put("description", "testing purpose");

ResponseBody = 
given().
        accept(ContentType.JSON).
        contentType(ContentType.JSON).
        header("userid", "131987”).
        body(jsonAsMap).
when().
        post("").
thenReturn().body();