如何在放心的头文件中传递授权令牌?

2022-09-04 08:13:34

尝试使用放心自动执行 API 测试

@Test
public void Login() {
    Response resp = given().
            body("{\"phone_number\":\"2222222222\",\"\r\n" + 
                    "               + \" \"country_code\": \"+91\",\"\r\n" + 
                    "               + \" \"login_type\": 0}").
            when().
            contentType(ContentType.JSON).
            post("http://url/api/v1/login");

    System.out.println(resp.asString());
}

答案 1

添加授权标头。

Response resp = given().header("Authorization", "Bearer "+token).body(...

有关详细信息,请参阅此处。


答案 2

First Create Method as httpHeaderManager()

为标头创建 Header 类的对象,并将其存储到 ArrayList 中,例如

public static Headers httpHeaderManager(){
    Header contentType = new Header("Content-Type","application/json");
    Header authorization = new Header("Authorization", "your token");
    List<Header> headerList = new ArrayList<Header>();
    headerList.add(contentType);
    headerList.add(authorization);
    Headers header = new Headers(headerList);

}

第二次调用 httpHeaderManager() 方法

@Test
public void create(){
  Response response = 
            given()
            .headers(httpHeaderManager())

}


推荐