Dritan
Dritan

Reputation: 1

OkHttp Api call to Okta Api end point hits {"successful":true,"redirect":false} instead of actual dataset

I am attempting to call okta to get the user info API endpoint with the Okhttp library. The application received {"successful": true,"redirect": false} when the call from java spring boot, instead of the actual dataset from the API endpoint using Postman. What am i missing in this case:

                Request requestValue = new Request.Builder()
                        .url("https://dev-xxxxxxx.okta.com/api/v1/users/xxxxxx")
                        .addHeader("Accept-Encoding", "gzip, deflate, br")
                        .addHeader("Accept", "application/json")
                        .addHeader("Content-Type", "application/json")
                        .addHeader("Authorization", "SSWS " + apiKey.getCfgValue()).build();
                
                try (Response response = httpClient.newCall(requestValue).execute()) {
                    if (response.code() == 200) {
                        return response;
                    }
                }

Appreciate much that anyone could help.

Upvotes: 0

Views: 483

Answers (2)

黄崇杰
黄崇杰

Reputation: 1

Because Response is actually a byte stream, you need to convert the byte stream into a string in order to truly access the response information. You can refer to the following code to handle the Response object.

String res = Optional.ofNullable(httpResponse.body())
                    .map(ResponseBody::byteStream)
                    .map(in -> {
                        try {
                            return IOUtils.toString(in, StandardCharsets.UTF_8);
                        } catch (IOException e) {
                            throw new BusinessException(ErrorEnum.CHAT_GPT_ERROR, e);
                        }
                    })
                    .orElse(null);

Upvotes: 0

calm
calm

Reputation: 43

Response response = httpClient.newCall(requestValue).execute()
**response.body()**

That's what you want.

Upvotes: 0

Related Questions