java 如何使用Rest Assured在API请求中添加承载令牌

jvidinwx  于 2022-12-17  发布在  Java
关注(0)|答案(1)|浏览(150)

这是一行代码,我想在其中添加"Bearer,然后从另一个名为config、id为静态变量的类文件添加id标记
请求报头("授权","+配置id +"");
我想这样通过("授权","承载者etytydfgdddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddd id是一个静态变量,其中已经存储了token,但我还想在token之前传递token type

esbemjvw

esbemjvw1#

你可以这样做:

public class Exampe {
    private static final String TOKEN = "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9";
    private static final String BASE_URI = "https://mywebsite.com";

    public static void main(String[] args) {
        RestAssured.given()
                .baseUri(BASE_URI)
                .header("Authorization", "Bearer " + TOKEN)
                .contentType(ContentType.JSON)
                .log().all()
                .when()
                .get("resource");
    }
}

日志:

Request method: GET
Request URI:    https://mywebsite.com/resource
Proxy:          <none>
Request params: <none>
Query params:   <none>
Form params:    <none>
Path params:    <none>
Headers:        Authorization=Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9
                Accept=*/*
                Content-Type=application/json
Cookies:        <none>
Multiparts:     <none>
Body:           <none>

相关问题