POST request is in rest order

I'm pretty sure that for a post request containing a JSON body,

My mail request code: -

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

+6


source to share


3 answers


It looks like your server is expecting a request body, but you are sending data as request parameters. If I understood correctly, you want to send your data as JSON. The easiest way to do it is to use this approach:



Map<String, Object>  jsonAsMap = 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(jsonAsMap).
when().
        post("").
thenReturn().body();

      

+4


source


Check the full path for the POST URI. There is no body for the mail call, which is mandatory. Ur should look like this:

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

      



Then you can perform operations on the answer.

0


source


// Try this code, it helped me, might help you too.

    {
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
}

      

0


source







All Articles