Content-Type وترويسة Accept
تعرّف إلى سبب إعلان استجابات JSON عن application/json.
Content-Type وترويسة Accept درس مجاني في Flask Academy على CoddyKit. هذا هو الدرس 4 من أصل 4. يمكنك قراءة الدرس كاملاً أدناه مجاناً — ثم تمرن عليه مباشرة في المتصفح باستخدام محرر أكواد مدمج ومدرس ذكاء اصطناعي متاح 24/7. هذا الدرس جزء من مسار التعلم في Flask Academy، وتقدمك يتزامن عبر الويب وتطبيق CoddyKit. تتضمن دورة Flask Academy 4 دروس في المجموع.
بعض أجزاء هذا الدرس لم تُترجم بعد وتظهر باللغة الإنجليزية.
Headers Carry Meaning
A response is more than a body. HTTP headers describe that body, and two of them decide how JSON flows between client and server. 📨
What Content-Type Says
The Content-Type header announces the format of the body you are sending. For JSON replies its value is application/json.
Content-Type: application/jsonjsonify Sets It for You
You rarely set this header by hand. When you call jsonify, Flask attaches application/json automatically, so clients always parse correctly.
Why It Matters to Clients
A browser or library reads Content-Type to decide how to handle the body. Seeing application/json, it parses text into a usable object.
Wrong Type, Wrong Result
Return JSON text as plain text/html and clients may not parse it. The body looks right but the missing signal breaks automatic decoding.
What the Accept Header Says
The request can carry an Accept header that lists formats the client wants. It is a wish, telling your server which response type fits best.
Accept: application/jsonContent-Type vs Accept
Keep the pair straight: Content-Type describes the body in the current message, while Accept states what the client hopes to receive back.
Reading Accept in Flask
You can inspect the request to honor what the caller wants. The request.accept_mimetypes proxy reveals the formats listed in that header.
request.accept_mimetypes["application/json"]Content Negotiation
Serving the format a client asked for is called content negotiation. An API can return JSON or HTML for the same route based on Accept.
Request Bodies Have Types Too
Incoming requests also carry Content-Type. When a client sends application/json, Flask knows the body is JSON and request.get_json can parse it.
Trust the Defaults
For most JSON APIs, jsonify handling Content-Type is all you need. Reach for Accept only when one route must serve multiple formats.
Quick Check
Tell apart the two headers that shape JSON exchange.
Recap
You learned that Content-Type labels the body and Accept asks for a format, and that jsonify sets application/json so JSON clients just work. 🎉
الأسئلة الشائعة
هل درس «Content-Type وترويسة Accept» مجاني؟
نعم — نص درس «Content-Type وترويسة Accept» كامل متاح مجاناً هنا على الويب. لتمرينه بشكل تفاعلي (محرر أكواد مدمج ومدرس ذكاء اصطناعي متاح 24/7) وفتح باقي دورة Flask Academy، انتقل إلى CoddyKit PRO. تتضمن دورة Flask Academy 4 دروس في المجموع.
ماذا ستتعلم في «Content-Type وترويسة Accept»؟
تعرّف إلى سبب إعلان استجابات JSON عن application/json. تتمرن على Flask Academy مع أكواد عملية تشغلها مباشرة في المتصفح، ومدرس ذكاء اصطناعي متاح 24/7 يجيب على أسئلتك أثناء عملك.
هل أحتاج إلى خبرة سابقة لأبدأ Flask Academy؟
لا تُشترط خبرة سابقة. Flask Academy على CoddyKit منظم للمبتدئين حتى المتقدمين، لذا يمكنك البدء من هنا أو من البداية والتقدم بسرعتك الخاصة. هذا هو الدرس 4 من أصل 4.
كم من الوقت يستغرق درس «Content-Type وترويسة Accept»؟
معظم دروس CoddyKit تستغرق حوالي 5–10 دقائق. كل منها موجز وتفاعلي، لذا تحرز تقدماً مستمراً وتستأنف من حيث توقفت عبر الويب والتطبيق.
هل يمكنني كتابة وتشغيل أكواد في درس Flask Academy هذا؟
نعم. كل درس في Flask Academy يتضمن محرر أكواد مدمج، لذا تكتب وتشغل أكواداً حقيقية مباشرة في متصفحك وتحصل على تعليقات فورية من الذكاء الاصطناعي — بدون إعداد محلي.
جميع الدروس في هذه الدورة
- تحويل قاموس إلى JSON باستخدام jsonify
- إرجاع القوائم والبيانات المتداخلة
- ضبط رموز الحالة في ردود JSON
- Content-Type وترويسة Accept