Search code examples
javapostpostmanpojocreateuser

How do I get all four keyValue pairs using RestAssured for CreateUser Post request


Response using RestAssured doesn't include all four key value pairs, it works fine in Postman and returns all four values.

  • Bottom right is the response of my post CREATE USER query https://reqres.in/ Top left I have attempted to write a code that runs the same query using RestAssured
  • Bottom left shows the log.

enter image description here

UserTests Class
// Create user request number 7 (post request)
    System.out.println("test_get_single_user_by_ID_returns_http_404() - User Story 7  CREATE");
    
    Response createUser = (Response) given().queryParam("Content-Type", "application/json")
    .body(au)
    .when().log().all().post("/api/users")
    .then().log().all().assertThat().statusCode(201).extract().response();
    
    
    String createUserResponse = createUser.asString();
    
    System.out.println(createUserResponse);
    JsonPath js = ReUseableMethods.rawToJson(createUserResponse);
    System.out.println(au.getCreatedAt());
    au.getJob();
    System.out.println(js);

Response in console:

{"id":"117","createdAt":"2020-07-05T11:17:26.597Z"}

required response
{
    "name": "RAK",
    "job": "Automation testing",
    "id": "683",
    "createdAt": "2020-06-26T07:36:28.264Z"
}

Solution

  • The problem is with your Content Type setting. Content type must be passed as a header and not as a queryParam.

    This will return the full object:

      Response createUser = (Response) given().header("Content-Type", "application/json")
                    .body(au)
                    .when().log().all().post("https://reqres.in/api/users")
                    .then().log().all().assertThat().statusCode(201).extract().response();