首页 > 解决方案 > 如何使用放心的方式做出正确的获取请求?

问题描述

我确实获取对 api 的请求

公共类 Req1 {

public static void main(String[] arg) {
    RestAssured.config = RestAssured.config().encoderConfig(encoderConfig().appendDefaultContentCharsetToContentTypeIfUndefined(false));
    Response response = given()
            .header("Accept", "application/json")
            .header("PWT", "123123123123")
            .header("Referer", "https://xxxxxxx.ru/")
            .header("Sec-Fetch-Mode", "cors")
            .header("X-Auth-Token", "123123123123")
            .header("X-User-Lang","rus")
            .body("dateEnd=2019-09-17&dateStart=2019-09-17&limit=100&officeCode=270&offset=0&onlyEmpty=false&typeBasis=\n")
            .baseUri("https://xxxxx.ru")
            .get();
    System.out.println(response.body().asString());
}

}

但请求不使用正文 -> 我得到的结果没有 dateEnd、officeCode 等

标签: rest-assured

解决方案


你可以这样使用。这将打印请求。如您所见, Body 确实包含您要发送的内容。

我认为,代码在您编写时正在工作。请检查身体是否正确。

Response response = given().header("Accept", "application/json").header("PWT", "123123123123")
                .header("Referer", "https://xxxxxxx.ru/").header("Sec-Fetch-Mode", "cors")
                .header("X-Auth-Token", "123123123123").header("X-User-Lang", "rus")
                .body("dateEnd=2019-09-17&dateStart=2019-09-17&limit=100&officeCode=270&offset=0&onlyEmpty=false&typeBasis=\n")
                .baseUri("https://xxxxx.ru").log().all().get();
        System.out.println(response.body().asString());

推荐阅读