如何使用放心的方式做出正确的“获取请求”?

时间:2019-09-18 02:18:25

标签: rest-assured

我确实收到了对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等

2 个答案:

答案 0 :(得分:0)

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

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

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

答案 1 :(得分:0)

正确的致电方式:

package api.restassured.libarary.basics.problems;

import io.restassured.RestAssured;
import io.restassured.builder.RequestSpecBuilder;
import io.restassured.config.RestAssuredConfig;
import io.restassured.response.Response;
import io.restassured.specification.RequestSpecification;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Test;

import java.util.HashMap;
import java.util.Map;

import static io.restassured.RestAssured.given;
import static io.restassured.RestAssured.when;
import static io.restassured.config.EncoderConfig.encoderConfig;

public class getCall {

    public static  RequestSpecification requestSpecification;
    @BeforeMethod
    public void setRequestSpecification(){

        Map<String ,String> hearders = new HashMap<String, String>(){{
                put("Accept", "application/json");
                put("PWT", "123123123123");
                put("Referer", "https://xxxxxxx.ru/");
                put("Sec-Fetch-Mode", "cors");
                put("X-Auth-Token", "123123123123");
                put("X-User-Lang","rus");
        }};

        RestAssuredConfig restAssuredConfig = new RestAssuredConfig();
        restAssuredConfig.encoderConfig(encoderConfig().
                                    appendDefaultContentCharsetToContentTypeIfUndefined(false));


         requestSpecification = new RequestSpecBuilder().
                                                    addHeaders(hearders).
                                                    setConfig(restAssuredConfig).
                                                    setBaseUri("https://xxxxx.ru").build();
    }


   @Test
    public void getCall(){

      String requestBody = "dateEnd=2019-09-17&dateStart=2019-09-17&limit=100&officeCode=270&offset=0&onlyEmpty=false&typeBasis=";

      Response response = given().
                              spec(requestSpecification).
                              body(requestBody).
                          when().
                                get().
                          then().
                              extract().response();

       System.out.println(response.body().asString());

   }



}