代码之家  ›  专栏  ›  技术社区  ›  Bharath S

如何在Rest assured中传递头中的授权令牌?

  •  13
  • Bharath S  · 技术社区  · 8 年前

    @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());
    }
    
    2 回复  |  直到 5 年前
        1
  •  23
  •   Nabin Bhandari    8 年前

    添加授权标头。

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

    有关更多信息,请参阅 here.

        2
  •  0
  •   Jalpit Patel    4 年前

    为标头创建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);
    
    }
    

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

    }