Kotlin – Get API data using OkHttp

This is probably a very basic process but it’s something I’ve struggled to do in Kotlin as the vast majority of examples are shown using Java. Whilst I would have been able to get my head around that code given time, the process would have been a lot simpler had I seen a Kotlin version instead. So I’ve made this post.

The sole goal of this post is to be able to connect to an external API and retrieve its data. This post won’t cover parsing the data in any form other than a String – parsing will come in a later post. For now I just want to get some data in!

If you want to go straight to the code, it lives here.

Setup

First we set up a very basic project using Spring Initializer. I set this up as a Gradle project and will be using the Kotlin DSL (Domain Specific Language) for the build file.

Coming from Javascript land I tended to use the inbuilt ‘fetch’ function to call an API so I didn’t need to pull in any dependencies. However, I have been told many times that it’s best to use a client like OkHttp when using Kotlin for web development.

To be able to use OkHttp we need to bring it in as a dependency. This is done through the build.gradle.kts file in the dependencies block:

Adding OkHttp Dependency to build.gradle.kts file

When we import our gradle project we’ll then have access to OkHttp and the methods it brings for making a simple API call.

Code

Next we create a Repository class which will hold the code for calling the API (note it doesn’t need to have a Repository annotation).

Kotlin code to call an external API using OkHttp

Main aspects

  • Create an instance of OkHttpClient (in the makeRequest() function) so that we can access its methods;
  • Create a request – similar code can be found on the OkHttp home page in Java. Here we create a new request using the Request.Builder() and pass it a URL to hit. As we don’t need any further information for this API we can just build the request. However you may need to use the .addHeader method, for example, to pass extra information to the API;
  • Execute the new API call – In our makeRequest() function we are doing a few things. First we’re taking the request we built. Then we’re asking OkHttp to make a new API call, and then for it to execute that API call;
  • Parse the data – As discussed, for the time being we’re just parsing this data as a String.

Other things to note

  • I’ve used @Scheduled annotation above our makeRequest() function. This is a Spring annotation that calls the function after initialisation of the bean properties. It get called one the beans have been instantiated and the initial delay has passed. The fixedDelay tells the function how long to wait until it next runs.
  • The @Scheduled requires the @EnableScheduling annotation above the class to work.
  • We’re printing the output to the terminal through the ‘println(parsedResponse)’ call within makeRequest(). This is just to prove we’ve got a response.
  • We’re using the demo version of the API-Football held on RapidAPI. If you need football data this is a great place to look.

And here’s the output:

API call printed out to console

At the moment this isn’t greatly useful as we’ll want to parse out the data and manipulate/store that information. However, we’ve managed to get data in from an external API which is a start!

I hope that this post is useful to some beginner Kotliners. Check back in soon where we’ll parse that data into a more manageable format.

Further reading

https://github.com/square/okhttp

If you enjoyed this type of content and would like to see more, feel free to check out my social media accounts:

Twitter: @sdrobertsCode
LinkedIn
GitHub