【问题标题】:Rest Assured: How do I return JSON response as a String? (Java)放心:如何将 JSON 响应作为字符串返回? (爪哇)
【发布时间】:2020-07-05 22:53:57
【问题描述】:

如何使用我的代码将 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);
}

[已添加 - 显示我如何使用下面 cmets 的解决方案]

我如何调用此方法的示例

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.

【问题讨论】:

标签: java rest-assured rest-assured-jsonpath


【解决方案1】:

只需编写一个简单的可重用方法来使用 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);
}

你可以使用这个获得任何价值

【讨论】:

  • 感谢您的回复。我将您的解决方案添加到我正在处理的场景中(代码中的左侧 cmets)以显示代码流。我不确定如何将您提供的内容应用到auth.getValueField().sendKeys("Token " + **NEED ACCESS TOKEN AS STRING**);。您能否提供更多指导?
  • 检查更新的部分,同时从你的代码中删除 getAccessToken() 并使用我给出的代码
  • 啊,我明白了。据我了解,getToken 参数设置为String。使用extract().asString(),然后我们将提取的响应作为字符串返回。凉爽的。我注意到一件事。您提供的可重用方法由于某种原因没有被调用,我已经更新了我的示例代码以供观察,如果有任何问题请告诉我。
  • 您能拨入我的 webex 吗?
  • 好的,让我看看怎么做。
猜你喜欢
  • 1970-01-01
  • 2020-06-04
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-02-22
  • 1970-01-01
  • 1970-01-01
  • 2013-08-25
相关资源
最近更新 更多