0

I want to implement a basic example using OkHttpClient. In my case I want to make Rest requests to https://data.brreg.no/rofs/od/rofs/stottetildeling/search?language=nob&mottakerOrgnr=987&fraDato=2016-11-20

In order to get public data. More information:

https://data.brreg.no/enhetsregisteret/api/docs/index.html#_oversikt https://www.brreg.no/produkter-og-tjenester/apne-data/register-offentlig-stotte/api

I implemented this code:

OkHttpClient client = new OkHttpClient();

Request request = new Request.Builder()
        .url("https://data.brreg.no/rofs/od/rofs/stottetildeling/search?language=eng")
        .get()
        .build();

Response response = client.newCall(request).execute();
System.out.println(response.body().string());

But I get this result:

IGVsbGVyIGF2Z2lmdHNmb3JkZWwiLCJzdG90dGVpbnN0cnVtZW50RnJpdGVrc3QiOm51bGwsInN0b3R0ZW1vdHRha2VyU3RvcnJlbHNlIjoiTGl0ZW4gb2cgTWVsbG9tc3RvciIsIm5hZXJpbmciOiI0MS4yIiwibmFlcmluZ0Jlc2tyaXZlbHNlIjoiT3BwZsO4cmluZyBhdiBieWduaW5nZXIiLCJmeWxrZXNudW1tZXIiOjE4LCJmeWxrZSI6Ik5vcmRsYW5kIiwic3RvdHRlZ2l2ZXJPcmdhbmlzYXNqb25zbnVtbWVyIjoiOTc0NzYxMDc2Iiwic3RvdHRlZ2l2ZXJOYXZuIjoiU0tBVFRFRVRBVEVOIiwic3RvdHRlZ2l2ZXJzUmVmZXJhbnNlIjpudWxsLCJ1bG92bGlnU3RvdHRlIjpudWxsfSx7InRpbGRlbGluZ0lkIjoi............

Looks like I have to configure the encoding properly in order to read the data. what is the proper way to implement this?

Peter Penzov
  • 1,126
  • 134
  • 430
  • 808
  • I tried with Java 8 Maven project with dependency com.squareup.okhttp3:okhttp:4.9.1. I used the same code in your post and I see data printed in console – Vasanth Subramanian Sep 06 '21 at 13:11
  • I use Java 11 with Coretto JVM – Peter Penzov Sep 06 '21 at 13:20
  • Just an observation: That looks like a Base 64 encoded string, which decodes to JSON (containing what appears to be Norwegian text). See [here](https://www.freeformatter.com/base64-encoder.html). – andrewJames Sep 06 '21 at 14:31
  • ok, how I can decode it to readable text? – Peter Penzov Sep 06 '21 at 14:34
  • You can research that question - for example: [Decode Base64 data in Java](https://stackoverflow.com/questions/469695/decode-base64-data-in-java). But I think it's probably better to figure out why it's encoded that way in the first place (i.e. fix the root cause problem, rather than using a workaround). I'd be surprised if the response is _supposed_ to arrive that way - but maybe that is correct. Can these responses contain binary data, such as images? – andrewJames Sep 06 '21 at 14:39
  • Is it possible to configure the encoding into OkHttpClient? – Peter Penzov Sep 06 '21 at 14:59
  • Sorry, I don't know. But it sounds like you have a new (and much more specific) question you can now research - and a new question you can ask, if you need to. (I'd be surprised if this has not already been asked and answered on SO.) – andrewJames Sep 06 '21 at 15:03
  • Can you please check what output you get for below code snippet? MediaType mediaType = response.body().contentType(); System.out.println("Media Type: " + mediaType.type()); System.out.println("Media Subtype: " + mediaType.subtype()); – Vasanth Subramanian Sep 06 '21 at 17:47
  • I get `Media Type: application Media Subtype: json` – Peter Penzov Sep 06 '21 at 18:24
  • Media Type and Subtype looks good. I too get the same. Wondering why you don't get the json output string. One last try, can you pls share the dependency tree. If you use Maven, can you please share the output of this - mvn dependency:tree – Vasanth Subramanian Sep 06 '21 at 18:30
  • I use Gradle: https://pastebin.com/9ajc534X – Peter Penzov Sep 06 '21 at 18:33

0 Answers0