0

Request being sent: '''@Test public void firstPost() { RestAssured.baseURI = "https://reqres.in/";

    JSONObject reqBody = new JSONObject();
     reqBody.put("name", "Ajay");
     reqBody.put("job", "leader");
    
    RequestSpecification reqSpec = RestAssured.given()
            .body(reqBody);
    Response res = reqSpec.post("/api/users");
    
    System.out.println(res.asString());
    
}'''

Response being printed is: {"id":"403","createdAt":"2020-08-28T18:13:32.294Z"}

Expected response { "name": "Ajay", "job": "leader", "id": "403", "createdAt": "2020-08-28T18:13:32.294Z" }

1 Answers1

0

You should add contentType like below.

@Test
public void firstPost() {
    RestAssured.baseURI = "https://reqres.in/";

    JSONObject reqBody = new JSONObject();
    reqBody.put("name", "Ajay");
    reqBody.put("job", "leader");
    
    RequestSpecification reqSpec = RestAssured.given()
            .contentType("application/json") // <-- add this
            .body(reqBody);
    Response res = reqSpec.post("/api/users");
    
    System.out.println(res.asString());
    
}

See the official wiki for more info about Content Type

Peter Quan
  • 788
  • 4
  • 9
  • Hi, @Test-Again , if this is the solution to your problem, please [Accept the answer](https://meta.stackexchange.com/questions/5234/how-does-accepting-an-answer-work) or [Vote up it](https://stackoverflow.com/help/privileges/vote-up) :) – Peter Quan Sep 09 '20 at 03:16