You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在MaxSoft IntelliAPI Framework中保存登录Token以授权其他API请求?

Got it, let's walk through how to implement this login flow with the MaxSoft IntelliAPI Framework— I’ve used this framework for API automation before, so here’s a practical, step-by-step approach that works:

1. Create the Login Request to Fetch Access Token

First, you’ll need to build and send the login POST request, then extract the token from the successful response. Here’s a concrete code example:

public void performLoginAndStoreToken() {
    // 1. Build your login request payload (adjust the JSON structure to match your API)
    String loginRequestBody = "{\"username\":\"test_user\",\"password\":\"test_pass123\"}";

    // 2. Send the POST request to your login endpoint
    Response loginResponse = ApiRequestHandler.sendPostRequest("https://your-api-domain/auth/login", loginRequestBody);

    // 3. Verify the login request was successful (adjust status code if your API uses a different one)
    Assert.assertEquals(loginResponse.getStatusCode(), 200, "Login request failed unexpectedly");

    // 4. Extract the access token from the response (update the JSON path to match your API's response structure)
    String accessToken = loginResponse.jsonPath().getString("data.access_token");

    // 5. Store the token in MaxSoft's global data holder for reuse
    TestDataHolder.setData("AUTH_TOKEN", accessToken);
}
2. Reuse the Stored Token in Other API Requests

MaxSoft IntelliAPI provides the TestDataHolder class to share data across test methods or test classes. You can retrieve the token from this holder and add it to the authorization header of subsequent requests:

public void fetchUserProfile() {
    // 1. Retrieve the stored access token
    String authToken = TestDataHolder.getData("AUTH_TOKEN");

    // 2. Set up the authorization header (adjust the auth scheme if your API uses Basic Auth instead of Bearer)
    Map<String, String> requestHeaders = new HashMap<>();
    requestHeaders.put("Authorization", "Bearer " + authToken);

    // 3. Send the authenticated GET request
    Response profileResponse = ApiRequestHandler.sendGetRequest("https://your-api-domain/users/profile", requestHeaders);

    // 4. Validate the response status
    Assert.assertEquals(profileResponse.getStatusCode(), 200, "Failed to fetch user profile with authenticated token");
}
Key Notes to Remember
  • Adjust JSON Paths: Make sure the jsonPath().getString() parameter matches exactly where your API returns the access token (e.g., some APIs return it directly under access_token instead of data.access_token).
  • Auth Scheme: If your API uses a different authorization method (like API keys in headers), modify the header key-value pair accordingly.
  • Test Execution Order: Ensure the login method runs before any authenticated API tests—you can use test annotations (like @BeforeClass in TestNG) to enforce this order.

内容的提问来源于stack exchange,提问作者Gayanjani Gamage

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.25 04:04:58