使用RestAssured,我得到了部分回应不确定我到底想念什么

问题描述

正在发送请求: '''@测试 公共无效的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());
    
}'''

正在打印的响应为: {“ id”:“ 403”,“ createdAt”:“ 2020-08-28T18:13:32.294Z”}

预期的响应 { “ name”:“ Ajay”, “ job”:“ leader”, “ id”:“ 403”, “ createdAt”:“ 2020-08-28T18:13:32.294Z” }

解决方法

您应该添加如下所示的contentType。

@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());
    
}

有关{strong>内容类型

的更多信息,请参见official wiki