[英]How to add Bearer token in API rerquest using Rest Assured
This is a line of code where I want to add "Bearer and then id token from another class file which name is config and id is static variable这是一行代码,我想在其中添加“Bearer,然后是来自另一个类文件的 id 令牌,该类文件的名称是 config,id 是静态变量
request.header("Authorization", ""+CONFIG.id+""); request.header("授权", ""+CONFIG.id+"");
I want to pass like this ("Authorization", "Bearer etytydfgdddddddddddddddddddddddd");我想这样传递 ("Authorization", "Bearer etytydfgdddddddddddddddddddddddddddd"); id is a static variable where token is already store but I want to pass token type as well before token
id 是一个静态变量,其中已经存储了令牌,但我也想在令牌之前传递令牌类型
You can do it like this:你可以这样做:
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");
}
}
Log:日志:
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>
You can pass the token using the auth().oauth2(token) method.您可以使用 auth().oauth2(token) 方法传递令牌。 This way, you won't have to explicitly use "Authorization" and "Bearer" text.
这样,您就不必明确使用“授权”和“不记名”文本。 Rest Assured will do the job.
放心将完成这项工作。 Refer the code below:
参考下面的代码:
public class HowToSendBearerToken {
private static final String myToken = "enter_your_token_value_here";
public static void main(String[] args) {
RestAssured.given()
.auth().oauth2(myToken)
.when()
.get("end_point");
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.