أول طلب GET لكم
جلب JSON من API في التعليمات البرمجية المشتركة
أول طلب GET لكم درس مجاني في Kotlin Multiplatform Academy على CoddyKit. هذا هو الدرس 2 من أصل 4. يمكنك قراءة الدرس كاملاً أدناه مجاناً — ثم تمرن عليه مباشرة في المتصفح باستخدام محرر أكواد مدمج ومدرس ذكاء اصطناعي متاح 24/7. هذا الدرس جزء من مسار التعلم في 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/7) وفتح باقي دورة Kotlin Multiplatform Academy، انتقل إلى CoddyKit PRO. تتضمن دورة Kotlin Multiplatform Academy 4 دروس في المجموع.
ماذا ستتعلم في «أول طلب GET لكم»؟
جلب JSON من API في التعليمات البرمجية المشتركة تتمرن على Kotlin Multiplatform Academy مع أكواد عملية تشغلها مباشرة في المتصفح، ومدرس ذكاء اصطناعي متاح 24/7 يجيب على أسئلتك أثناء عملك.
هل أحتاج إلى خبرة سابقة لأبدأ Kotlin Multiplatform Academy؟
لا تُشترط خبرة سابقة. Kotlin Multiplatform Academy على CoddyKit منظم للمبتدئين حتى المتقدمين، لذا يمكنك البدء من هنا أو من البداية والتقدم بسرعتك الخاصة. هذا هو الدرس 2 من أصل 4.
كم من الوقت يستغرق درس «أول طلب GET لكم»؟
معظم دروس CoddyKit تستغرق حوالي 5–10 دقائق. كل منها موجز وتفاعلي، لذا تحرز تقدماً مستمراً وتستأنف من حيث توقفت عبر الويب والتطبيق.
هل يمكنني كتابة وتشغيل أكواد في درس Kotlin Multiplatform Academy هذا؟
نعم. كل درس في Kotlin Multiplatform Academy يتضمن محرر أكواد مدمج، لذا تكتب وتشغل أكواداً حقيقية مباشرة في متصفحك وتحصل على تعليقات فورية من الذكاء الاصطناعي — بدون إعداد محلي.
جميع الدروس في هذه الدورة
- إضافة Ktor واختيار محركات المنصة
- أول طلب GET لكم
- POST والعناوين ومعلمات الاستعلام
- المهلات والسجلات وعناوين Base URLs