0

The following code snap is for a REST call with OkHttp3 in Java.

    ObjectMapper objectMapper = new ObjectMapper();
    OkHttpClient client = new OkHttpClient();
    Request request = new Request.Builder()
        .url(myUrl)
        .build();
    Call call = client.newCall(request);
    try (Response response = call.execute()) {
        String json = response.body().toString();
        JsonNode node = objectMapper.readTree(json);  // <--- cause an error
        if(node.has("foo") && node.get("foo").has("boo")) {
            node = node.get("foo").get("boo");
            for(int i = 0; i < node.size(); i++) {
                result.add(this.parseJson(node.get(i)));
            }
             ...
        }
        return Optional.empty();
    } catch (IOException e) {
        log.error(e.getMessage());
    }

The readTree method throws an error as the following:

Unrecognized token 'okhttp3': was expecting (JSON String, Number, Array, Object or token 'null', 'true' or 'false')_ at [Source: (String)"okhttp3.internal.http.RealResponseBody@3bb3d2d4"; line: 1, column: 8]

I don't see "okhttp3" when I run curl for the Url and I can't see the response data in a readable format. I don't use the readValue method because I only need a small portion of the response and the response contains a reserved word. How to inspect the response and resolve this issue?

vic
  • 2,548
  • 9
  • 44
  • 74

0 Answers0