首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何使用rest-assured发出正确的Get请求?

如何使用rest-assured发出正确的Get请求?
EN

Stack Overflow用户
提问于 2019-09-18 10:18:26
回答 2查看 423关注 0票数 0

我确实收到了api的请求

公共类Req1 {

代码语言:javascript
复制
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等

EN

回答 2

Stack Overflow用户

发布于 2019-09-19 01:41:41

您可以像这样使用。这将打印请求。正如你所看到的,Body包含了你正在发送的内容。

我认为,代码可以像你写的那样工作。请检查正文是否正确。

代码语言:javascript
复制
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());
票数 0
EN

Stack Overflow用户

发布于 2019-10-11 03:05:38

实现调用的正确方式:

代码语言:javascript
复制
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());

   }



}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/57984194

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档