简体   繁体   English

如何使用 Rest Assured 在位置响应标头字符串中提取身份验证代码

[英]How to extract an auth code in Location response header String using Rest Assured

Currently I have a method that does some authorization, which is used for my access token POST request in the body to establish the proper access token.目前我有一个方法可以进行一些授权,它用于我在正文中的访问令牌 POST 请求以建立正确的访问令牌。 In my authorization method, I am able to extract the Location header value in the response.在我的授权方法中,我能够在响应中提取 Location 标头值。 The only issue is that I only need to extract the values between code= and & as seen in the below string ( locationHeaderForAuthCode ):唯一的问题是我只需要提取code=&之间的值,如下面的字符串( locationHeaderForAuthCode )所示:

https://something.url.com/myapp/auth/token?code=**12WVUcPyFbmTZUOcgaluGl98r08**&iss=https%3A%2F%2Fsomething.url.com%3A8443%2Fam%2Foauth2%2Fvendors&client_id=5c1d7ex3-g3rc-4a64-9855-074a57cc1570 https://something.url.com/myapp/auth/token?code=**12WVUcPyFbmTZUOcgaluGl98r08**&iss=https%3A%2F%2Fsomething.url.com%3A8443%2Fam%2Foauth2%2Fvendors&client_id=5c1d7ex3-g3rc-4a64 -9855-074a57cc1570

Since that code is used for the request body in my access token POST request, I need to extract only that portion.由于该代码用于我的访问令牌 POST 请求中的请求正文,因此我只需要提取该部分。 How do I achieve this in Rest Assured, and if not, then just using plain Java?我如何在 Rest Assured 中实现这一点,如果没有,那么只需使用纯 Java?

Any help would be appreciated.任何帮助,将不胜感激。 Below is my method for authorization that sets the Location header, and the other method for the access token post request:下面是我设置 Location 标头的授权方法,以及访问令牌发布请求的另一种方法:

private static void getAuthorizeCode() throws JsonException {

    response = given()
                .config(RestAssured.config()
                    .encoderConfig(EncoderConfig.encoderConfig()
                            .encodeContentTypeAs("x-www-form-urlencoded",
                                    ContentType.URLENC)))
            .cookies(cookies)
            .header("Accept-Encoding", "gzip, deflate, br")
            .formParam("redirect_uri", devRedirectUrl)
            .formParam("response_type", responseType)
            .formParam("client_id", userClientID)
            .formParam("client_secret", userClientSecrets)
            .formParam("decision", decision)
            .formParam("csrf", tokenId)
            .post(urlProps.getProperty("devVGWAuthorizeUrl"))
            .then()
            .assertThat()
            .statusCode(302)
            .extract().response();

    locationHeaderForAuthCode = response.getHeader("Location");

}

private static void getAccToken() throws JsonException {
    response = given()
            .log().all().config(RestAssured.config()
                    .encoderConfig(EncoderConfig.encoderConfig()
                            .encodeContentTypeAs("x-www-form-urlencoded",
                                    ContentType.URLENC)))
            .cookies(cookies)
            .header("Accept-Encoding", "gzip, deflate, br")
            .formParam("grant_type", "authorization_code")
            .formParam("client_id", userClientID)
            .formParam("client_secret", userClientSecrets)
            .formParam("redirect_uri", devRedirectUrl)
            .formParam("code", locationHeaderForAuthCode)
            .post(urlProps.getProperty("devVGWAccessTokenUrl"))
            .then()
//                .assertThat()
//                .statusCode(200)
            .log().all()
            .extract().response();

    accToken = response.jsonPath().getString("access_token");
    System.out.println("Access token is " + accToken);
}

You just need simple split() method of String to extract value from this text.您只需要 String 的简单split()方法即可从此文本中提取值。

String url = "https://something.url.com/myapp/auth/token?code=**12WVUcPyFbmTZUOcgaluGl98r08**&iss=https%3A%2F%2Fsomething.url.com%3A8443%2Fam%2Foauth2%2Fvendors&client_id=5c1d7ex3-g3rc-4a64-9855-074a57cc1570";
String[] texts = url.split("code=");
String code = texts[1].split("&")[0];
System.out.println(code); //**12WVUcPyFbmTZUOcgaluGl98r08**

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用通用代码从 REST Assured 响应中提取 cookie - How to extract cookies from a REST Assured response using common code 如何使用Rest-Assure确保标头位置响应与正则表达式匹配 - how to match header location response with regular expression using rest-assured 如何将访问令牌值从一个 API 响应主体(在一个类中)提取到另一个 API 标头(在另一个类中)在 rest 保证代码中 - How to extract accesss token value from one API response body(in one class) into another API header(in another class) in rest assured code 如何验证响应并从 Rest Assured 中的响应正文中提取值? - How to validate a response and extract a value from Response body in Rest Assured? 使用REST-assured验证响应头中的整数值 - Verify integer value in response header using REST-assured 如何使用放心从具有多个命名空间的 SOAP XML 响应中提取价值? - How to extract value from SOAP XML response with multiple namespaces using Rest-assured? 放心:从响应列表中提取值 - Rest Assured: extract value from Response List 从 rest 中提取一些字段保证响应 - extract some field from rest assured response Rest 保证:如何将 JSON 响应作为字符串返回? (爪哇) - Rest Assured: How do I return JSON response as a String? (Java) 如何使用放心将响应日志保存在变量中? - How to save response log in variable using rest assured?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM