Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

POST request in rest assured

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....

like image 771
Akshay Rajesh Dwivedi Avatar asked Aug 07 '15 10:08

Akshay Rajesh Dwivedi


1 Answers

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", "[email protected]");
map.put("description", "testing purpose");

ResponseBody = 
given().
        accept(ContentType.JSON).
        contentType(ContentType.JSON).
        header("userid", "131987").
        body(map).
when().
        post("").
thenReturn().body();
like image 124
Johan Avatar answered Oct 19 '22 16:10

Johan