*New to Rest Assured
Question: How do I pass values provided from endpoint 'A' to endpoint 'B'?
My scenario: (using a UI api service that provides curls)
{
"session": "Need to pass this session Id which changes everytime for every signin",
"challengeName": "CUSTOM_CHALLENGE",
"challengeSentTo": "example@EMAIL.com",
"username": "This id never changes"
}
Curl for endpoint 2:
curl -X GET "https://example.com/apiservice/m1/users/verifyCode" -H "accept: application/json" -H "session: id from GET user/signin" -H "username: id from GET user/signin" -H "X-API-KEY: api key needed"
My Code for GET user/sign in (works for GET user/signin). Need to get this to work for Step 3
public void getInitialAuthSignIn() {
.given()
.header("X-API-KEY", "apiKey in here")
.queryParam("challengeMethod", "EMAIL")
.header("alias", "example@EMAIL.com")
.when()
.log().all()
.get(baseUri + basePath + "/users/signin");
}
You can run the initial sign in API call first and set the sessionId, username from the response as class variables. Then use them in all other API calls you want.
import io.restassured.path.json.JsonPath;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Test;
import static io.restassured.RestAssured.given;
public class AuthResponseTest {
private static final String baseUri = "https://ExampleApiService.com";
private static final String basePath = "/apiservice/m1";
private static final String verifyPath = "/users/verifyCode";
private static final String usenameId = "fixed_id_never_changes";
private String sessionId;
private String userName;
@BeforeMethod // Use the appropriate TestNG annotation here.
public void getInitialAuthSignIn() {
JsonPath jsonPath = given()
.contentType("application/json")
.header("X-API-KEY", "apiKey in here")
.queryParam("challengeMethod", "EMAIL")
.header("alias", "example@EMAIL.com")
.when()
.log().all()
.get(baseUri + basePath + "/users/signin")
.then()
.extract()
.jsonPath();
this.sessionId = jsonPath.get("session");
this.userName = jsonPath.get("username");
}
@Test
public void testVerifyCode() {
given()
.header("X-API-KEY", "apiKey in here")
.header("session", this.sessionId)
.header("username", this.userName)
.when()
.log().all()
.get(baseUri + basePath + "/users/verifyCode")
.then()
.extract()
.response()
.prettyPrint();
}
}
I have used the TestNG annotations here.