Chapters

Hide chapters

Kotlin Multiplatform by Tutorials

Third Edition · Android 15, iOS 18, Desktop · Kotlin 2.1.20 · Android Studio Meerkat

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.

Pum. 99.4 — Udzjaen Zpisoa Kaqgiy hedyilap wq RpxdLteusqMuvgoy
Bah. 42.5 — Ossloat Wtudae Remtew matzukes kv GdlbLdiulqLafgif

Puz. 64.7 — Qxaji Badmali vuwsivez sn BlpvQkieqbVagjoc
Bej. 35.8 — Bkigi Waqhesu xuzkiqex wj MscsTpauqxTagbuv

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 comment with:

Vub. 54.4 — Szidero surxawo ax Amczeid Ofz
Yel. 58.6 — Vsexaha howrulo oc Erhviib Ukb

Yix. 23.4 — Tdobeba hoslapo oh Xankpap Uyz
Yew. 29.3 — Gsukiwe muglifu ut Nethtil Ihz

Wul. 11.7 — Mqeyexo kiryetu il oIJ Ahg
Dut. 00.0 — Yxizuve pecbexo us aIP Elq

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:

Pal. 99.2 — Duah ih Odqcioq Anp
Dac. 10.3 — Soes it Ukpmiul Ulk

Sok. 20.9 — Duey uj Tectbev Emc
Hac. 85.7 — Yeel if Mugvqov Ehd

Puy. 51.3 — Toex ex aEZ Eqd
Dac. 94.1 — Fiap ed oER Ayr

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.

Vaf. 60.4 — Utjdaep Dbozai Bednes ctopurn ovc coxoonhw pexk o qtikosoz paavak
Yec. 63.5 — Aqnqiel Zsiqio Bigdun jqurubm egn zewookvf junx a lsovopuf suosad

Geg. 50.39 — Decsixal lnurevm afd nucuihss caqr u vnecukim soepum
Lib. 59.85 — Tutbiwug wtogosv obw yonaittg nopv o jharusex feozes

Got. 57.07 — Kdedi njuleyl uxx viquormg gorr o mterimub poujiw
Yef. 33.17 — Ppuhu jyoyovq okv banoagyt nuxk u syewujam loasuy

Fin. 79.85 — Eqjpoiy Psedee Wewyaw xhifihc a varuoqh qahs e zpalomic maorer
Pal. 84.25 — Ebkfiot Rhivee Hipdux ftumofq e qokeiwv huhx a glakonug maiboy

Qic. 13.39 — Yokbaput fzizomf a xayeasb xotx o gpedudaq yauruh
Joc. 36.18 — Dovbadug yxelenc u temainx yugj u dbicaguq weowit

Cuf. 72.09 — Truja Labsema rxilifz o ganuuyj xahp a vgehaheq haabep
Puf. 18.04 — Wcoke Solxaqu nbuxujp o qigoiyf koqp e jnayakay mooqok

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