javarest-assured

Extract Response Body RestAssured


I had an API, need to post request in order to get access token. Below are the code:

private String getToken() {
        RestAssured.baseURI = "https://raudhahku.com.my/api/v1/oauth/token";

        requestSpecification = RestAssured.given();
        JSONObject requestParams = new JSONObject();
        requestParams.put("grant_type", "client_credentials");
        requestParams.put("client_id", 1);
        requestParams.put("client_secrets", "Gsv7brPlt5fIpqa1ebFc5A1uHwDJBPY6TRaMm8wa");

        requestSpecification.header("Content-Type", "application/json");
        requestSpecification.body(requestParams.toString());
        response = RestAssured.post();
        int statusCode = response.getStatusCode();
        System.out.println(statusCode);
        return response.body().jsonPath().getString("access_token");
    }

I don't know how to extract the access token from following data:

> {
>     "data": {
>         "token_type": "Bearer",
>         "expires_in": 850216799,
>         "access_token": "eyJ0eXAiOiJKV1QiLCJhbGciOiJSUzI1NiIsImp0aSI6ImM0Mjc0NmE1YWI3ZTk4ZWQwNjViZDc1Y2E3ZDQ1N2M5YzY3MjM0YTNiZjJjY2Q5ZTAxMDg1MzU0N2UyNGIzNDFkNWM2YmJmMjJiMmMxYjA3In0.eyJhdWQiOiIxIiwianRpIjoiYzQyNzQ2YTVhYjdlOThlZDA2NWJkNzVjYTdkNDU3YzljNjcyMzRhM2JmMmNjZDllMDEwODUzNTQ3ZTI0YjM0MWQ1YzZiYmYyMmIyYzFiMDciLCJpYXQiOjE3MDQyNDI0MDAsIm5iZiI6MTcwNDI0MjQwMCwiZXhwIjoyNTU0NDU5MTk5LCJzdWIiOiIiLCJzY29wZXMiOltdfQ.BeCx2TZ3YUJtvbFIqaUC6pbVc6oVK2BC0dRHH4NX9Uepx29kwDHSrh3BkvwJ-qH5N6nXen0eUJA4oGTqkxppT9MmGIQBSZPS_w5CRCfaaPIFhzRM0ZMkCjtUCL2kUB3nG289pGUIoKNZALcOV4d34zui0U7o7HCls6BTgffaHg3-f6dh6oYvvrQ5AowuzoV6KsembWVeeLQBVcalgSInBn7BOzM4d1UCe3-bpwAm_iGCICmaSAa1ciep1EpwJfpgfdKGf-8g71LuQTrJLU6a_TGxZ7xr4N-UxwB0wjqLjjGOkgPas2onnNiJ9Nt2nfdQmw1zAhcFfszMaLIgX8qeygfp8JZIKj-Npq3CZo6sUh0di7IfnxbuSlkiz9pW5NEsORK_73P_2-459Ul1Mg3gUJ3a88X5HukzqSQBpEZn279-JSCCWGR_7ogczKAi_l6IcnJMfia8D8XVx41BqcrvXoX3uPC36EqxUJRfbvZtnCys8vj8H6a6DVdmaC9ojkS6FmMLHKkOD7phHvOLySpB3xDAYeMLQOZhR3KrMj2tm2MK88kfFR0tL4O9IUknEAGwzummjR-NYfgojh-Pl0mc9Tkj8nHaA5WNyfFbkjZn5wDxnAj9ayhHsyaRX8RyVG3fjV4D0WAR3JKb5CzvzwkuNjjGpH3xWwv7EEgUNS8XtKM"
>     } }

EDIT: Please help me on deal with integer in Rest Assured been misintepret as String.


Solution

  • There are two things that we need to rectify.


    First, there is a typo in the construction of the json-object:

    requestParams.put("client_secrets", "Gsv7brPlt5fIpqa1ebFc5A1uHwDJBPY6TRaMm8wa");
    

    should be:

    requestParams.put("client_secret", "Gsv7brPlt5fIpqa1ebFc5A1uHwDJBPY6TRaMm8wa");
    

    (plural vs singular)


    Next, when we make the actual request, we need to base the request on the requestParams we constructed. This line:

    response = RestAssured.post();
    

    should therefore be changed to:

    response = requestSpecification.post();
    

    With those changes, we can run the testGetAllLanguages-test and it will succeed.

    You can find a patch with the changes listed above here (gist.github.com).


    I suggest refactoring the code and make use of RestAssure's fluent API. A refactoring may look like this:

    private String getToken() {
        // @formatter:off
        return RestAssured
            .given()
                .header("Content-Type", "application/json")
                .body(new JSONObject()
                    .put("grant_type", "client_credentials")
                    .put("client_id", 1)
                    .put("client_secret", "Gsv7brPlt5fIpqa1ebFc5A1uHwDJBPY6TRaMm8wa")
                    .toString())
            .when().post("https://raudhahku.com.my/api/v1/oauth/token")
            .then()
                .statusCode(is(200))
                .body("$", hasKey("data"))
                .body("data", hasKey("access_token"))
                .extract().body().path("data.access_token");
        // @formatter:on
    }