api token authentication code example

Example 1: which authentication you used

I use bearer tokens in my current framework.
I send a get request to special API endpoint by providing valid 
credentials, then it will return Access Token. I use that token 
in my request header and access other API endpoints

Example 2: how to get token in api

We can use .extract then .jsonPath and lastly getString("token")

String myToken =
                
        given()
                .log().all()
                .contentType(ContentType.URLENC)
                .formParam("email","xxxxxxxxxxx")
                .formParam("password","xxxxxxx")
        .when()
                .post("/login")
        .then()
                .log().all()
                .statusCode(200)
                .body("token", is(notNullValue()))
                .contentType(ContentType.JSON)
                .body("token",is(not(emptyString())))
                .extract()
                .jsonPath()
                .getString("token")
        ;

Tags:

Misc Example