POST request fails (rest-assured test)

Purple

I have problem with making POST request with rest-assured.

This code works:

given().contentType(ContentType.JSON).body("{\"key\": \"val\"}").    
        when().post(url + resource).then().assertThat().statusCode(200).body("otherVal", equalTo(otherVal)); 

But I was trying to use param() or parameter() methods like that:

This one gives:

given().parameter("key", "val").                                      
        when().post(url + resource).then().assertThat().statusCode(200);  

Expected status code <200> doesn't match actual status code <415>.

This:

    given().parameter("key", "val").                                                         
            when().post(url + resource).then().assertThat().body("otherVal", equalTo(otherVal));  

java.lang.IllegalStateException: Expected response body to be verified as JSON, HTML or XML but no content-type was defined in the response. Try registering a default parser using: RestAssured.defaultParser(<parser type>);

And This:

RestAssured.defaultParser = Parser.JSON;                                                   
given().parameter("key", "val").                                                       
        when().post(url + resource).then().assertThat().body("otherVal", equalTo(otherVal));

java.lang.IllegalArgumentException: The JSON input text should neither be null nor empty

I have run out of ideas whats wrong.

What I'm trying to do is to avoid writing full jsons for all test, it will be faster if I could skip all "" and {}. Is my approach correct?

Johan

Let's look at your first example:

given().contentType(ContentType.JSON).body("{\"key\": \"val\"}").    
        when().post(url + resource).then().assertThat().statusCode(200).body("otherVal", equalTo(otherVal));

What happens here is that you put { "key" : "val" } (as text) into the body of the request. This text happens to be JSON. From REST Assured's perspective you might as well could have put { "key" : "val" which is not valid JSON. Your server responds correctly since the server requires and understands JSON. It understands that the body should be JSON since you passing JSON as content-type.

So let's look at your second example:

given().parameter("key", "val").                                      
        when().post(url + resource).then().assertThat().statusCode(200);

Here your service returns 415 because you're missing the JSON content-type. What happens when you use param or parameter with POST is that you create form parameters. Form parameters are also sent in the request body BUT form parameters are not JSON! Specifying "key" and "val" as form parameter like you do will be the same as this:

given().contentType("x-www-form-urlencoded").body("key=val").when().url + resource).then().assertThat().statusCode(200);

So in your second example there's actually two problems:

  1. You're not sending JSON
  2. You have the wrong content-type

And because of (2) you get 415 from the server

Moving on to your third example:

given().parameter("key", "val").                                                         
            when().post(url + resource).then().assertThat().body("otherVal", equalTo(otherVal));

What (probably) happens here is that your server doesn't contain a response body because it expects the request to include "application/json" as content-type. So there is no body to assert (the request is wrong)! The response only contains the 415 status (line) as a header.

Which leads us to your last example:

RestAssured.defaultParser = Parser.JSON;                                                   
given().parameter("key", "val").                                                       
        when().post(url + resource).then().assertThat().body("otherVal", equalTo(otherVal));

Here you instruct REST Assured to treat a missing content-type as JSON but the problem (again) is that your server doesn't return any response body at all so this won't help.

Solution:

You should put a supported JSON object-mapping framework (Jackson, Faster Jackson, Simple JSON or Gson) in your classpath (for example jackson-databind) and just create a map as described in the documentation:

Map<String, Object>  jsonAsMap = new HashMap<>();
map.put("key", "val");

given().
        contentType(ContentType.JSON).
        body(jsonAsMap).
when().
        post(url + resource).
then().
        statusCode(200).
        body("otherVal", equalTo(otherVal)); 

Since creating maps in Java is quite verbose I usually do something like this if I have nested maps:

given().
        contentType(ContentType.JSON).
        body(new HashMap<String,Object>() {{
             put("key1, "val1");
             put("key2, "val2");
             put("key3", asList("val3", "val4"));
             put("nested", new HashMap<String,String>() {{
                 put("key4", "val4");
                 put("key5", "val5");
             }});
        }}).
when().
        post(url + resource).
then().
        statusCode(200).
        body("otherVal", equalTo(otherVal)); 

Or you create a DTO representation of your data and just pass an object to REST Assured:

MyDTO myDTO = new MyDTO(...);
given().
        contentType(ContentType.JSON).
        body(myDTO).
when().
        post(url + resource).
then().
        statusCode(200).
        body("otherVal", equalTo(otherVal)); 

You can read more in the object-mapping documentation.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Spring Mock mvc rest-assured test fails to get status

From Dev

Internal Server Error when making POST request using Rest Assured

From Dev

How to frame the rest assured script for a post request based on CURL

From Dev

How do i send JsonObject with nested values as Post request in REST assured

From Dev

Rest Assured :- Getting 404 response from Post request created using Pathparam and FormParam

From Dev

Empty respond body for post with rest assured

From Dev

CORS Post Request Fails

From Dev

CORS Post Request Fails

From Dev

Rest-assured. Is it possible to extract value from request json?

From Dev

Rest Assured API testing - Pass a Json Object as parameter to a get request

From Dev

Rest Assured API testing - Pass a Json Object as parameter to a get request

From Dev

How do you run Rest assured against a test database?

From Dev

Using PowerMock to mock static class in a rest-assured test

From Dev

Rest-assured simple test with Google Maps API

From Dev

REST Assured: how to test object of type "Set" in body?

From Dev

Integrating jayway rest assured with serenity rest assured

From Dev

Integrating jayway rest assured with serenity rest assured

From Dev

Multiple instances of REST Assured

From Dev

Rest assured with digest auth

From Dev

Condition in Rest-Assured

From Dev

Assertion for Xml in Rest Assured

From Dev

Rest Assured - Json Extract

From Dev

Spring REST controller post request

From Dev

POST Request empty in REST service

From Dev

Post Request Django REST Framework

From Dev

Spring data rest POST request

From Dev

Rest web services post request

From Dev

Test and stub params in POST request

From Dev

how I can use global header request through all tests using rest assured

Related Related

  1. 1

    Spring Mock mvc rest-assured test fails to get status

  2. 2

    Internal Server Error when making POST request using Rest Assured

  3. 3

    How to frame the rest assured script for a post request based on CURL

  4. 4

    How do i send JsonObject with nested values as Post request in REST assured

  5. 5

    Rest Assured :- Getting 404 response from Post request created using Pathparam and FormParam

  6. 6

    Empty respond body for post with rest assured

  7. 7

    CORS Post Request Fails

  8. 8

    CORS Post Request Fails

  9. 9

    Rest-assured. Is it possible to extract value from request json?

  10. 10

    Rest Assured API testing - Pass a Json Object as parameter to a get request

  11. 11

    Rest Assured API testing - Pass a Json Object as parameter to a get request

  12. 12

    How do you run Rest assured against a test database?

  13. 13

    Using PowerMock to mock static class in a rest-assured test

  14. 14

    Rest-assured simple test with Google Maps API

  15. 15

    REST Assured: how to test object of type "Set" in body?

  16. 16

    Integrating jayway rest assured with serenity rest assured

  17. 17

    Integrating jayway rest assured with serenity rest assured

  18. 18

    Multiple instances of REST Assured

  19. 19

    Rest assured with digest auth

  20. 20

    Condition in Rest-Assured

  21. 21

    Assertion for Xml in Rest Assured

  22. 22

    Rest Assured - Json Extract

  23. 23

    Spring REST controller post request

  24. 24

    POST Request empty in REST service

  25. 25

    Post Request Django REST Framework

  26. 26

    Spring data rest POST request

  27. 27

    Rest web services post request

  28. 28

    Test and stub params in POST request

  29. 29

    how I can use global header request through all tests using rest assured

HotTag

Archive