简体   繁体   English

如何使用 Rest Assured 在 API 请求中添加 Bearer 令牌

[英]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.

相关问题 如何使用REST确保自动获取Bearer令牌 - How to automatically get Bearer Token using REST Assured 如何使用 Rest Assured 从 KeyCloak 服务获取承载令牌 - How to get Bearer Token from KeyCloak Service using Rest Assured 如何使用Rest Assured将数组列表添加到Rest API请求? - How to add array list to rest API request using Rest Assured? 如何从get api获取csrf令牌并使用REST Assured将csrf令牌传递给另一个post api? - How to get csrf token from get api and pass csrf token to another post api using REST Assured? Rest Assured Bearer 认证 - Rest Assured Bearer authentication 如何使用Rest-Assured请求POST API发送令牌和主体值? - How to request a POST API send token and body values using Rest-Assured? 如何使用uid密钥和私有密钥来检索令牌来实现Java Rest-Assured API身份验证? - How to implement Java Rest-Assured API authentication using the uid key and secret key to retrieve token? 如何使用REST方法和REST方法获取REST API终结点的访问令牌,当前出现404错误? - How to get the access token of REST API endpoint using Rest Assured with POST method, Currently I am getting 404 error? 如何使用放心测试需要身份验证的 Rest API - How to test Rest API which require authentication using Rest assured 如何在放心的标头中传递授权令牌? - How to pass authorization token in header in Rest assured?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM