簡體   English   中英

Rest 保證:如何將 JSON 響應作為字符串返回? (爪哇)

[英]Rest Assured: How do I return JSON response as a String? (Java)

如何使用我的代碼將 JSON 響應作為字符串返回?

目的:我想在getAccessToken()從 json 響應正文中獲取 accessToken 后調用它,並且需要將其作為String返回以用於其他方法。

我試圖獲得的響應示例:

"accessToken" : "The ID I need from here"

代碼:

private String apiAccessToken;

public JsonPath getAccessToken() {
    JsonPath jsonPath = given().header("X-API-KEY", Config.API_KEY).header("session", this.sessionID)
            .header("username", this.userNameId).queryParam("code", verifiedCode).log().all()
            .get(baseUri + basePath + "/vm1/verifyCode").then().log().all().extract().jsonPath();

    this.apiAccessToken = jsonPath.get("accessToken");
    return new JsonPath(apiAccessToken);
}

[已添加 - 顯示我如何使用以下評論中的解決方案]

我如何調用此方法的示例

public static String getToken(String key) {
    String res = given()
            .header("X-API-KEY", Config.API_KEY)
            .header("session", this.SessionId)
            .header("username", this.UserName)
            .queryParam("code", verifiedCode)
            .log().all()
            .get(baseUri + basePath + "/vm1 /verifyCode")
            .then()
            .log().all()
            .extract().asString();

    JsonPath js = new JsonPath(res);
    return js.get(key).toString();
}

public static String getJsonPath(Response response, String key) {
    String complete = response.asString();
    JsonPath js = new JsonPath(complete);
    return js.get(key).toString();
}

@Test
    public void testAuthValidator() throws InterruptedException, IOException, GeneralSecurityException {
        String sentCode = GmailUtility.getVerificationCode(); // Uses GMAIL API service to to read and get code from email and sends to getAccessToken
        System.out.println(sentCode);
        String Token = getToken("accessToken"); // Validates verification code. Spits out response for accessToken
        System.out.println(validator);
        driver = initializeDriver(); // Invokes Chrome
        driver.get(env.API_Website); // Goes to api website
        AuthApi auth = new AuthApi(driver);
        auth.getAuthorizeButton().click(); // Clicks a text field on webpage
        auth.getValueField().sendKeys("Token " + Token); // Need to call extracted response for the accessToken from getAccessToken.

只需編寫一個簡單的可重用方法來使用 JSONPath 提取值並在代碼中調用該方法,這是一個示例

可重復使用的方法:

public static String getJsonPath(Response response, String key) {
    String complete = response.asString();
    JsonPath js = new JsonPath(complete);
    return js.get(key).toString();
}

測試:

public static void main(String[] args) {

    Response res = given().header("X-API-KEY", Config.API_KEY).header("session", this.sessionID)
            .header("username", this.userNameId).queryParam("code", verifiedCode).log().all()
            .get(baseUri + basePath + "/vm1/verifyCode").then().log().all().extract().response();
    String value = getJsonPath(res, "accessToken");
    
    System.out.println(value);
}

更新:

public static String getToken(String key) {
    String res = given().header("X-API-KEY", Config.API_KEY).header("session", this.SessionId)
            .header("username", this.UserName).queryParam("code", verifiedCode).log().all()
            .get(baseUri + basePath + "/vm1 /verifyCode").then().log().all().extract().asString();
    JsonPath js = new JsonPath(res);
    return js.get(key).toString();
}

@Test
public void testAuthValidator() throws InterruptedException, IOException, GeneralSecurityException {
    String sentCode = GmailUtility.getVerificationCode();
    System.out.println(sentCode);
    String Token = getToken("accessToken");
    System.out.println(Token);
    driver = initializeDriver();
    driver.get(env.API_Website);
    AuthApi auth = new AuthApi(driver);
    auth.getAuthorizeButton().click();
    auth.getValueField().sendKeys("Token " + Token);
}

您可以使用它獲得任何價值

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM