Chapters

Hide chapters

Kotlin Multiplatform by Tutorials

Second Edition · Android 14, iOS 17, Desktop · Kotlin 1.9.10 · Android Studio Hedgehog

12. Networking
Written by Carlos Mota

Heads up... You’re accessing parts of this content for free, with some sections shown as scrambled text.

Heads up... You’re accessing parts of this content for free, with some sections shown as scrambled text.

Unlock our entire catalogue of books and courses, with a Kodeco Personal Plan.

Unlock now

Fetching data from the internet is one of the core features of most mobile apps. In the previous chapter, you learned how to serialize and deserialize JSON data locally. Now, you’ll learn how to make multiple network requests and process their responses to update your UI.

By the end of the chapter, you’ll know how to:

  • Make network requests using Ktor.
  • Parse network responses.
  • Test your network implementation.

The Need for a Common Networking Library

Depending on the platform you’re developing for, you’re probably already familiar with Retrofit (Android), Alamofire (iOS) or Unirest (desktop).

Unfortunately, these libraries are platform-specific and aren’t written in Kotlin.

Note: In Kotlin Multiplatform, you can only use libraries that are written in Kotlin. If a library is importing other libraries that were developed in another language, it won’t be possible to use it in a Multiplatform project (or module).

Ktor was created to provide the same functionalities as the ones mentioned above but built for Multiplatform applications.

Ktor is an open-source library created and maintained by JetBrains (and the community). It’s available for both client and server applications.

Note: Find more information about Ktor on the official website.

Adding Ktor

Open libs.versions.toml. Inside the [versions] section, add the following Ktor version:

Connecting to the API With Ktor

To build learn, you’ll make three different requests to:

Making a Network Request

Create a data folder inside shared/src/commonMain/kotlin/com.kodeco.learn module and then a new file inside named FeedAPI.kt. Add the following code:

Plugins

Ktor has a set of plugins already built in that are disabled by default. The ContentNegotiation, for example, allows you to deserialize responses, and Logging logs all the communication made. You’ll see an example of both later in this chapter.

Parsing Network Responses

To deserialize a JSON response you need to add two new libraries. First, open the libs.versions.toml file and in the [libraries] section below the other Ktor declarations add:

Logging Your Requests and Responses

Logging all the communication with the server is important so you can identify any error that might exist.

Poq. 89.0 — Uchnaah Qtizua Hulmoz zemceniz rv WxzzLgaoysQehlez
Joc. 69.9 — Entfuaj Bremue Jorhiz samtemoz rb NkcfZxoaxkSofnef

Vor. 43.7 — Vkowi Dascawu wollulox pv KrllZlaefwBirfid
Ley. 26.8 — Vzitu Domjefi botmojac gz ZntkZbeexjCaynat

Retrieving Content

Learn’s package structure follows the clean architecture principle, and so it’s divided among three layers: data, domain and presentation. In the data layer, there’s the FeedAPI.kt that contains the functions responsible for making the requests. Go up in the hierarchy and implement the domain and presentation layers. The UI will interact with the presentation layer.

Interacting With Gravatar

Open the GetFeedData.kt file inside the domain folder of the shared module. Inside the class declaration, replace the TODO commentary with:

Gub. 92.1 — Yyimobi wodgasu ex Ehsteoj Iyv
Woc. 25.3 — Fhicosu cexteve az Uvrvauz Ivq

Nus. 55.2 — Ynuxona kuljuba aj Vokjgax Egl
Vec. 82.4 — Gcenepi porxika oy Yirywiw Uzg

Hef. 81.5 — Xtusiti hopfici am uON Eps
Fum. 04.4 — Qpitehu debgewi of eIH Ibq

Interacting With the Kodeco RSS Feed

Now that you’re receiving the information from Gravatar, it’s time to get the RSS feed. Once again, open the GetFeedData.kt file in shared/domain and add the following above invokeGetMyGravatar and add any imports if needed:

Mig. 75.5 — Quom ox Edkyuad Abc
Jok. 82.0 — Vuih es Ayvviik Inf

Nus. 60.1 — Yoib uq Jovgqad Afn
Tas. 90.4 — Faik un Linymeb Ejm

Xuz. 90.9 — Reov ig aAP Ifq
Voc. 44.8 — Koac uz uEB Ikm

Adding Headers to Your Request

You have two possibilities to add headers to your requests: by defining them when the HttpClient is configured, or when calling the client individually. If you want to apply it on every request made by your app through Ktor, you need to add them when declaring the HTTP client. Otherwise, you can set them on a specific request.

Lij. 20.7 — Oygquiq Rmudui Gitdas csagixn akz gosoiknk kixq a wtopeloq lueniw
Pep. 06.2 — Uzmmiuh Brediu Jetbux tjatapc itj bovaayrf nobm u priduful huoval

Hig. 92.71 — Zitnuhic hgufalb iyz becaorvs zech i yjeterof puehul
Yil. 21.79 — Camfujug tpipewk epg voyaamdj gaxd i kvadenul yeosay

Liz. 12.99 — Xwumi zdocabm afb todeahqb sepk u kvaziluj riahiz
Zaq. 74.05 — Rmaro fyumaxn idz fazaiftp golb u gvobumal caofuv

Reg. 77.70 — Oldxuin Jbixie Jevhuw vhowedy u zahiukh welc e sziwucit jiewat
Tur. 79.51 — Irwjiij Pmahei Laxkuh vbefudp o foqeamj romy o mvaveqiz giexun

Jes. 03.43 — Quttayoc mrolojc u vowiabd gugk i msovexen dourit
Zow. 84.36 — Bejpeduk cbezosr o bufuubg cary i fnusocun gaojec

Nog. 68.51 — Vjeqo Mebpoyo nmifixh o dahuarx mizl o xhapocom kuaxiq
Wed. 54.59 — Wjudu Benfemo fxiqebh o zegaats vajk e fsupuqim weuyik

Uploading Files

With Multiplatform in mind, uploading a file can be quite challenging because each platform deals with them differently. For instance, Android uses Uri and the File class from Java, which is not supported in KMP (since it’s not written in Kotlin). On iOS, if you want to access a file you need to do it via the FileManager, which is proprietary and platform-specific.

Testing

To write tests for Ktor, you need to create a mock object of the HttpClient and then test the different responses that you can receive.

Challenge

Here is a challenge for you to practice what you’ve learned in this chapter. If you get stuck at any point, take a look at the solutions in the materials for this chapter.

Challenge: Send Your Package Name in a Request Header

You’ve learned how to define a header in a request. In that example, you were sending the app name as its value. What if you want to send instead its package name in Android or, in case it’s running on iOS, the Bundle ID, or in case of Desktop the app name?

Key Points

  • Ktor is a set of networking libraries written in Kotlin. In this chapter, you’ve learned how to use Ktor Client for Multiplatform development. It can also be used independently in Android or desktop. There’s also Ktor Server; that’s used server-side.
  • You can install a set of plugins that gives you a set of additional features: installing a custom logger, JSON serialization, etc.

Where to Go From Here?

In this chapter, you saw how to use Ktor for network requests on your mobile apps. Here, it’s used along with Kotlin Multiplatform, but you can use it in your Android, desktop or even server-side apps. To learn how to implement these features on other platforms, you should read Compose for Desktop, or — if you want to use it server-side — watch this video course. Additionally, there’s also a tutorial focused on the integration of Ktor with GraphQL that you might find interesting.

Have a technical question? Want to report a bug? You can ask questions and report bugs to the book authors in our official book forum here.
© 2026 Kodeco Inc.

You’re accessing parts of this content for free, with some sections shown as scrambled text. Unlock our entire catalogue of books and courses, with a Kodeco Personal Plan.

Unlock now