最初のGETリクエスト
共有コードからAPIのJSONを取得します
「最初のGETリクエスト」はCoddyKit上の無料Kotlin Multiplatform Academyレッスンです。 これはレッスン2/4です。 下記で完全なレッスンを無料で読むことができます。その後、ブラウザ内の組み込みコードエディタと24時間対応のAIチューターでハンズオン演習できます。 これはKotlin Multiplatform Academy学習パスの一部であり、ウェブとCoddyKitアプリ全体で進捗が同期されます。 Kotlin Multiplatform Academyコースには全4レッスンが含まれています。
このレッスンの一部はまだ翻訳されておらず、英語で表示されています。
Fetch From Shared Code
With the client ready, you can now make your first GET request from commonMain and reuse it on both apps. 🚀
Network Calls Suspend
Every Ktor request is a suspend function, so it never blocks a thread and fits naturally into coroutines.
suspend fun load() { /* request here */ }Call get With a URL
The simplest request passes a URL string to client.get, which returns an HttpResponse you can read from.
val response = client.get("https://api.example.com/users")Read the Status Code
Check whether the call succeeded by reading response.status before you trust the body.
if (response.status.value == 200) { /* ok */ }Get the Body as Text
To read the payload, call bodyAsText, which suspends and returns the raw response as a String.
val json: String = response.bodyAsText()Typed Bodies Are Cleaner
Ktor can deserialize directly into a type using body, saving you from parsing the text by hand later.
val users: List<User> = response.body()One-Line GET
You can also skip the response object entirely and let get return your typed result in a single expression.
val users: List<User> = client.get(url).body()Wrap in a Function
Put the call inside a clean suspend function so both apps share the exact same fetching logic.
suspend fun fetchUsers() = client.get(url).body<List<User>>()Run It From a Scope
Because the call suspends, you launch it inside a coroutine scope rather than calling it from plain synchronous code.
scope.launch { val users = fetchUsers() }It Works on Both Apps
This single function now runs on Android and iOS with no changes, because the engine differences live below the API.
Keep Calls in the Repository
For real apps, hide raw get calls inside a repository so the UI never touches Ktor directly.
Quick Check
How do you read the raw response payload as a String?
Recap
A GET request is a suspend call: get the URL, check status, then read the body as text or a typed object. One function serves both apps.
よくある質問
「最初のGETリクエスト」レッスンは無料ですか?
はい。「最初のGETリクエスト」の完全なテキストはこのウェブで無料で読めます。インタラクティブに演習し(組み込みコードエディタと24時間対応のAIチューター)、Kotlin Multiplatform Academyコースの残りをアンロックするには、CoddyKit PROにアップグレードしてください。 Kotlin Multiplatform Academyコースには全4レッスンが含まれています。
「最初のGETリクエスト」で何を学びますか?
共有コードからAPIのJSONを取得します ブラウザで直接実行するハンズオンコードでKotlin Multiplatform Academyを演習し、24時間対応のAIチューターがレッスンを進める中での質問に答えます。
Kotlin Multiplatform Academyを始めるのに経験は必要ですか?
事前経験は必要ありません。CoddyKitのKotlin Multiplatform Academyは初級者から上級者向けに構成されているため、ここから始めるか最初から始めて、自分のペースで進むことができます。 これはレッスン2/4です。
「最初のGETリクエスト」レッスンにはどのくらい時間がかかりますか?
ほとんどのCoddyKitレッスンは約5~10分かかります。各レッスンはコンパクトでインタラクティブなので、着実に進歩し、ウェブとアプリ全体で正確に前回の場所から再開できます。
このKotlin Multiplatform Academyレッスンでコードを書いて実行できますか?
はい。すべてのKotlin Multiplatform Academyレッスンに組み込みコードエディタが含まれているため、ブラウザでリアルコードを書いて実行し、即座のAIフィードバックを取得できます。ローカル設定は不要です。