获取 HTTP 状态 400 – post 请求中的错误请求放心

Getting HTTP Status 400 – Bad Request in post request in rest assured

我正尝试放心地创建 POST 请求 java 但收到 HTTP 状态 400 – 错误请求。尝试了以下两种方法,相同的 API 在邮递员中运行良好。我用的是4.1.2放心pom.xml

curl --location --request POST 'http://localhost:8080//api/v1/planning/trips?pageNumber=1&pageSize=40'
--header 'authority: http://localhost:8080'
--header 'authorization: Bearer e8c108d6-c380-4715-849b-b6eccd8d2045'
--header 'origin: http://localhost:8080'
--header 'referer: http://localhost:8080'
--header 'Content-Type: application/json;charset=UTF-8'
--data-raw '{ "endPlacementTimestamp": 1594994096206, "startPlacementTimestamp": 1593525296206 }'

方法一:

            RestAssured.baseURI="http://localhost:8080";
            RequestSpecification httpRequest = RestAssured.given();
            httpRequest.header("authority","http://localhost:8080");
            httpRequest.header("authorization","Bearer e8c108d6-c380-4715-849b-b6eccd8d2045");
            httpRequest.header("Content-Type","application/json;charset=UTF-8");
            httpRequest.header("origin","http://localhost:8080");
            httpRequest.header("referer","http://localhost:8080");
            JSONObject requestParams = new JSONObject();
            requestParams.put("endPlacementTimestamp", "1594994096206");
            requestParams.put("startPlacementTimestamp","1593525296206");
            httpRequest.body(requestParams.toJSONString());
            Response response = httpRequest.request(Method.POST,"planning/trips?pageNumber=1&pageSize=40");
            int statusCode = response.getStatusCode();
//            Assert.assertEquals(statusCode, "200");
            // Retrieve the body of the Response
            ResponseBody body = response.getBody();

方法二:

            String body= "{\n" +
    "    \"endPlacementTimestamp\": 1594994096206,\n" +
    "    \"startPlacementTimestamp\": 1593525296206\n" +
    "}";
RestAssured.baseURI="http://localhost:8080//api/v1/";
Response response = given()
        .contentType("application/json")
        .header("authorization","Bearer e8c108d6-c380-4715-849b-b6eccd8d2045")
        .header("origin","http://localhost:8080")
        .header("referer","http://localhost:8080")
        .body(body)
        .post("planning/trips?pageNumber=1&pageSize=40");[![enter image description here][1]][1]

在方法 1 中:

  1. 输入 RestAssured.baseURI="http://localhost:8080//api/v1";
  2. 最后一行 .post("/planning/trips?pageNumber=1&pageSize=40"); 参考 https://www.toolsqa.com/rest-assured/post-request-using-rest-assured/

这是它的简化版本,我在这里添加了一个 JSONObject,这样您就不会在 body()

中硬编码有效负载
    RestAssured.baseURI = "http://localhost:8080";

    RequestSpecification requestSpec = new RequestSpecBuilder().addHeader("authority", "http://localhost:8080")
            .addHeader("authorization", "Bearer e8c108d6-c380-4715-849b-b6eccd8d2045")
            .addHeader("origin", "http://localhost:8080").addHeader("referer", "http://localhost:8080")
            .addHeader("Content-Type", "application/json").build();

    JSONObject payload = new JSONObject();
    body.put("endPlacementTimestamp", 1594994096206L);
    body.put("startPlacementTimestamp", 1593525296206L);

    given().log().all().spec(requestSpec).queryParam("pageNumber", "1").queryParam("pageSize", "40").body(payload)
            .post("/api/v1/planning/trips");