![](/img/trans.png)
[英]Update JSON response from an API and pass the updated response as input/body to another API using Rest Assured
[英]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
在一个 class 中生成一个令牌生成 API,给出以下响应。
{
"access_token": "eyJraWQiOiJNR2FOQUtYXC9pa0grNE1wTE9aS05wMGtqbXNOd0lzXC9WXC9EYm1LZ0pZdTZNPSIsImFsZyI6IlJTMjU2In0.eyJzdWIiOiIxYjBtcjc4cHNjMHIyM25nYnJqMml1MnNkNCIsInRva2VuX3VzZSI6ImFjY2VzcyIsInNjb3BlIjoic2dwZi5wcm9kdWN0XC",
"expires_in": 3600,
"token_type": "Bearer"
}
然后在另一个 class 中获取另一个 Get API,其中我想在 rest 保证代码中的 header 中提取此 access_token 值。 那么我怎样才能在另一个 class 中获取该访问令牌值呢?
public class Get_Service_List {
private Response response;
String BaseUrl = "https://dev.api.sgf.eus.nt/pro";
@Given("Get Service list API")
public void get_Service_list_API() {
RestAssured.baseURI = BaseUrl;
}
@When("call the API with valid token and details")
public void call_the_API_with_valid_token_and_details() {
response = RestAssured.given()
.header("Content-Type", "application/json")
.header("Authorization", "Bearer "+TokenGeneration.accessToken)
.when()
.get("/api/protsvc/ser");
}
@Then("validate the resonse body with list of services")
public void validate_the_resonse_body_with_list_of_services() {
String response_body = response.getBody().asString();
System.out.println("response is: " +response_body);
}
@Then("validate for 200 status code")
public void validate_for_status_code() {
int status_code = response.getStatusCode();
System.out.println("status is: " +status_code);
}
}
不太了解cucumber如何共享state。下面是从响应中提取access_token
的方法。
String accessToken = response.jsonPath().getString("access_token");
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.