ضبط رموز الحالة في ردود JSON
اربط أجسام JSON بالرموز 201 و404 وغيرها.
ضبط رموز الحالة في ردود JSON درس مجاني في Flask Academy على CoddyKit. هذا هو الدرس 3 من أصل 4. يمكنك قراءة الدرس كاملاً أدناه مجاناً — ثم تمرن عليه مباشرة في المتصفح باستخدام محرر أكواد مدمج ومدرس ذكاء اصطناعي متاح 24/7. هذا الدرس جزء من مسار التعلم في Flask Academy، وتقدمك يتزامن عبر الويب وتطبيق CoddyKit. تتضمن دورة Flask Academy 4 دروس في المجموع.
بعض أجزاء هذا الدرس لم تُترجم بعد وتظهر باللغة الإنجليزية.
Status Codes Tell the Story
Every HTTP response carries a status code that signals success or failure. Your JSON body explains the details, but the code sets the tone first. 🚦
The Default Is 200
When you return jsonify alone, Flask sends 200 OK. That is perfect for a successful read, but other actions deserve their own code.
Return a Tuple
To choose a code, return a tuple of the JSON response and the number. Flask reads the second element as the HTTP status to send.
return jsonify(message="created"), 201201 for Created
When a request creates a new record, reply with 201 Created. It tells the client the resource now exists, which 200 alone would not convey.
return jsonify(id=new_id), 201404 for Not Found
If the requested item does not exist, return a JSON body with 404. The client learns the resource is missing, not that the server broke.
return jsonify(error="user not found"), 404400 for Bad Input
When a client sends invalid data, answer with 400 Bad Request. Pair it with a JSON message that explains exactly what went wrong.
return jsonify(error="name is required"), 400The 2xx Family
Codes in the 2xx range mean success. 200 reads, 201 creates, and 204 signals success with no body to send back.
The 4xx Family
Codes in the 4xx range blame the client. 400 means bad input, 401 means unauthenticated, and 403 means forbidden.
The 5xx Family
Codes in the 5xx range blame the server. A 500 means your code raised an unhandled error while processing an otherwise valid request.
Adding Headers Too
You can extend the tuple to three parts: body, status, and a headers dict. That lets you attach extra metadata in one clean return.
return jsonify(ok=True), 201, {"X-Created": "now"}Consistency Builds Trust
Pick the right code for every outcome and use it everywhere. Consistent status codes make your API predictable and a joy to integrate.
Quick Check
Match the action to the most fitting HTTP status code.
Recap
You learned to pair JSON bodies with the right status code by returning a tuple, using 201, 404, and 400 to make your API clear and honest. ✅
الأسئلة الشائعة
هل درس «ضبط رموز الحالة في ردود JSON» مجاني؟
نعم — نص درس «ضبط رموز الحالة في ردود JSON» كامل متاح مجاناً هنا على الويب. لتمرينه بشكل تفاعلي (محرر أكواد مدمج ومدرس ذكاء اصطناعي متاح 24/7) وفتح باقي دورة Flask Academy، انتقل إلى CoddyKit PRO. تتضمن دورة Flask Academy 4 دروس في المجموع.
ماذا ستتعلم في «ضبط رموز الحالة في ردود JSON»؟
اربط أجسام JSON بالرموز 201 و404 وغيرها. تتمرن على Flask Academy مع أكواد عملية تشغلها مباشرة في المتصفح، ومدرس ذكاء اصطناعي متاح 24/7 يجيب على أسئلتك أثناء عملك.
هل أحتاج إلى خبرة سابقة لأبدأ Flask Academy؟
لا تُشترط خبرة سابقة. Flask Academy على CoddyKit منظم للمبتدئين حتى المتقدمين، لذا يمكنك البدء من هنا أو من البداية والتقدم بسرعتك الخاصة. هذا هو الدرس 3 من أصل 4.
كم من الوقت يستغرق درس «ضبط رموز الحالة في ردود JSON»؟
معظم دروس CoddyKit تستغرق حوالي 5–10 دقائق. كل منها موجز وتفاعلي، لذا تحرز تقدماً مستمراً وتستأنف من حيث توقفت عبر الويب والتطبيق.
هل يمكنني كتابة وتشغيل أكواد في درس Flask Academy هذا؟
نعم. كل درس في Flask Academy يتضمن محرر أكواد مدمج، لذا تكتب وتشغل أكواداً حقيقية مباشرة في متصفحك وتحصل على تعليقات فورية من الذكاء الاصطناعي — بدون إعداد محلي.
جميع الدروس في هذه الدورة
- تحويل قاموس إلى JSON باستخدام jsonify
- إرجاع القوائم والبيانات المتداخلة
- ضبط رموز الحالة في ردود JSON
- Content-Type وترويسة Accept