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