Lecture 03 · Application Programming Interfaces
المحاضرة 03 · واجهات برمجة التطبيقات
HTTP methods to CRUD, and anatomy of a request and response
طرق HTTP مقابل CRUD، وتشريح الطلب والاستجابة
P1 MUSTOne four-row table to memorise, then one request and one response to dissect until every word on them has a name.
جدول من أربعة صفوف تحفظه، ثم request واحد وresponse واحد تشرّحهما حتى يصبح لكل كلمة فيهما اسم.
Four methods, four operations
أربع طرق، أربع عمليات
Slides 8 and 9 map the application's CRUD operations onto HTTP methods. Slide 8 lists them GET first, slide 9 lists them in CRUD order. Same mapping:
تربط الشريحتان 8 و9 عمليات CRUD في التطبيق بـ HTTP methods. الشريحة 8 تبدأ بـ GET، والشريحة 9 ترتّبها بترتيب CRUD. والربط نفسه في الحالتين:
| HTTP methodHTTP method | CRUD | Action (slide 9)الفعل (الشريحة 9) | Safe?آمنة (safe)؟ | Idempotent?Idempotent؟ |
|---|---|---|---|---|
POST | Createإنشاء (Create) | create a new resourceإنشاء resource جديد | noلا | noلا |
GET | Readقراءة (Read) | return a resourceإرجاع resource | yesنعم | yesنعم |
PUT | Update / Replaceتحديث / استبدال (Update / Replace) | update a resourceتحديث resource | noلا | yesنعم |
DELETE | Deleteحذف (Delete) | delete a resourceحذف resource | noلا | yesنعم |
احفظ: POST = Create، وGET = Read، وPUT = Update / Replace، وDELETE = Delete. والعمودان الأخيران من محاضرة 01: GET وحده safe، وGET وPUT وDELETE كلها idempotent، أما POST فليس هذا ولا ذاك.
The last two columns come from Lecture 01 (page in-02). Safe means the request does not change server state: reading only. Idempotent means sending the request twice has the same intended effect as sending it once, so a client may retry it after a lost reply.
العمودان الأخيران من المحاضرة 01 (صفحة in-02). Safe تعني أن الطلب لا يغيّر حالة الخادم، أي قراءة فقط. وIdempotent تعني أن إرسال الطلب مرتين له نفس الأثر المقصود لإرساله مرة واحدة، فيستطيع العميل إعادته إذا ضاع الرد.
PUT /lamps/123with "state = on", sent twice: lamp is on. Same as once. Idempotent.DELETE /lamps/123sent twice: the lamp is gone after the first; the second changes nothing more (it may answer 404, but the server state is the same). Idempotent.POST /visitssent twice: two visits are created with two IDs. Not idempotent.
PUT /lamps/123 بقيمة "state = on" مرتين: المصباح يعمل، كأنها مرة واحدة، فهو idempotent. وDELETE /lamps/123 مرتين: يُحذف المصباح في المرة الأولى ولا يتغير شيء بعدها في الثانية (قد يكون الرد 404 لكن حالة الخادم نفسها)، فهو idempotent أيضًا. أما POST /visits مرتين فيُنشئ زيارتين بمعرّفين مختلفين، فهو ليس idempotent.
"POST posts something new; PUT puts it in place." POST adds a new item to a collection and the server picks its ID. PUT writes the full state of an item whose address you already know.
"POST يضيف شيئاً جديداً، وPUT يضع الشيء في مكانه." POST يضيف عنصراً جديداً إلى collection والخادم هو من يختار المعرّف. وPUT يكتب الحالة الكاملة لعنصر تعرف عنوانه مسبقاً.
The example: fetching http://localhost/index.html
المثال: جلب http://localhost/index.html
Slide 10 sets up the example. The client asks for http://localhost/index.html. The web server fetches the resource index.html from its file system. Here the resource is simply a static file. In the diagram, the only arrow between endpoint and resource is Read: this is a GET.
الشريحة 10 تجهّز المثال: العميل يطلب http://localhost/index.html، والـ web server يجلب الـ resource المسمّى index.html من نظام ملفاته. هنا الـ resource مجرد static file. وفي الرسم السهم الوحيد بين الـ endpoint والـ resource هو Read، أي أنه طلب GET.
Anatomy of the request (slide 11)
تشريح الطلب (الشريحة 11)
GET /index.html HTTP/1.1 Host: localhost Accept: text/html
| Pieceالجزء | Valueالقيمة | Name on the slideاسمه في الشريحة |
|---|---|---|
| first word of the request lineالكلمة الأولى في سطر الطلب | GET | HTTP methodHTTP method |
| the leading slashالشرطة المائلة في البداية | / | HTTP route (root)HTTP route (الجذر root) |
| the file after itالملف الذي بعدها | index.html | resourceresource |
| last word of the request lineالكلمة الأخيرة في سطر الطلب | HTTP/1.1 | protocol versionإصدار البروتوكول (protocol version) |
Host headerالـ header المسمّى Host | localhost | serverالخادم (server) |
Accept headerالـ header المسمّى Accept | text/html | the media type the client wants backنوع الوسائط (media type) الذي يريد العميل استلامه |
سطر الطلب فيه ثلاثة أجزاء: GET هو HTTP method، ثم /index.html حيث / هو HTTP route (الجذر) وindex.html هو الـ resource، ثم HTTP/1.1 وهو إصدار البروتوكول. وبعده الـ headers: Host: localhost يحدد الـ server، وAccept: text/html يحدد نوع المحتوى الذي يريده العميل.
Notice the server name is not in the request line. The client opened a connection to localhost already, and the Host header repeats the name (HTTP/1.1 requires it, because one machine can host many sites). A GET normally has no body.
لاحظ أن اسم الخادم غير موجود في سطر الطلب. العميل فتح الاتصال مع localhost مسبقاً، والـ Host header يكرر الاسم (وHTTP/1.1 يشترطه لأن جهازاً واحداً قد يستضيف عدة مواقع). وطلب GET عادة ليس له body.
Anatomy of the response (slide 12)
تشريح الاستجابة (الشريحة 12)
HTTP/1.1 200 OK Content-Type: text/html Content-Length: 500 <html> ... </html>
- Status line:
HTTP/1.1(version),200(status code),OK(status message). The code is what programs check; the message is a reason phrase for humans.سطر الحالة:
HTTP/1.1هو الإصدار، و200هو status code، وOKهو status message. البرامج تقرأ الرمز، والرسالة نص موجّه للبشر. - Header fields:
Content-Type: text/htmlsays the body is HTML.Content-Length: 500says the body is 500 bytes long, so the client knows where the message ends.حقول الـ header:
Content-Type: text/htmlتعني أن الـ body من نوع HTML. وContent-Length: 500تعني أن طول الـ body هو 500 بايت، فيعرف العميل أين تنتهي الرسالة. - Blank line, then the body: the actual content of index.html, from
<html>to</html>.سطر فارغ، ثم الـ body: المحتوى الفعلي لملف index.html من
<html>إلى</html>.
Slide 12 puts the status line inside the brace labelled Header. Strictly, HTTP calls it the status line and the two lines under it the header fields, but for this course "header" means everything above the blank line.
الشريحة 12 تضع سطر الحالة داخل القوس المسمّى Header. بدقة، HTTP يسمّيه status line ويسمّي السطرين تحته header fields، لكن في هذا المقرر كلمة header تعني كل ما فوق السطر الفارغ.
Suppose the server returns the body <p>Hi</p> in plain ASCII. What is Content-Length?
لنفترض أن الخادم أرجع الـ body التالي <p>Hi</p> بترميز ASCII. كم قيمة Content-Length؟
<p> 3 bytes Hi 2 bytes </p> 4 bytes total 3 + 2 + 4 = 9 -> Content-Length: 9
The status line and the headers are not counted. If the body were UTF-8 Arabic text, each Arabic letter takes 2 bytes, so count bytes, not characters.
سطر الحالة والـ headers لا تُحسب. ولو كان الـ body نصاً عربياً بترميز UTF-8 فكل حرف عربي يأخذ 2 بايت، فاحسب البايتات لا الحروف.
Status codes worth recognising
رموز حالة تستحق أن تعرفها
The first digit is the class: 2xx success, 3xx redirect, 4xx client error, 5xx server error. Only 200 OK is on the slides; the rest are context so you can reason about the smart-home error case on the next page.
الرقم الأول يحدد الفئة: 2xx نجاح، و3xx إعادة توجيه، و4xx خطأ من العميل، و5xx خطأ من الخادم. الشرائح تذكر 200 OK فقط، والباقي للسياق حتى تفهم حالة الخطأ في مثال المنزل الذكي في الصفحة التالية.
1. POST vs PUT for create. Create = POST. PUT = Update / Replace. If an answer maps PUT to Create or POST to Update, it is wrong for this course.
1. POST أم PUT للإنشاء؟ الإنشاء = POST، وPUT = Update / Replace. أي إجابة تربط PUT بالإنشاء أو POST بالتحديث خاطئة في هذا المقرر.
2. Header vs body. Content-Type and Content-Length are headers that describe the body. The HTML itself is the body.
2. header أم body؟ Content-Type وContent-Length هي headers تصف الـ body، أما الـ HTML نفسه فهو الـ body.
3. Status code vs status message. 200 is the code, OK is the message. "Which part of HTTP/1.1 200 OK is the status message?" Answer: OK.
3. status code أم status message؟ 200 هو الرمز وOK هي الرسالة. فإذا سُئلت أي جزء من HTTP/1.1 200 OK هو status message؟ الجواب: OK.
4. Content-Length is bytes of the body only, not characters, not lines, and not the whole message.
4. Content-Length هو عدد بايتات الـ body فقط، وليس عدد الحروف ولا الأسطر ولا طول الرسالة كاملة.
5. Route vs resource in slide 11. The route is / (root) and the resource is index.html. The server is localhost, from the Host header.
5. الـ route أم الـ resource في الشريحة 11؟ الـ route هو / (الجذر)، والـ resource هو index.html، والـ server هو localhost من Host header.
Check yourself
اختبر نفسك
Which HTTP method maps to the CRUD operation Create?
أي HTTP method يقابل عملية Create في CRUD؟
A. POST = Create, GET = Read, PUT = Update/Replace, DELETE = Delete (slides 8 and 9). B is the standard distractor; PUT is Update/Replace in this course.
A. POST = Create، وGET = Read، وPUT = Update/Replace، وDELETE = Delete (الشريحتان 8 و9). والخيار B هو المشتّت المعتاد، فـ PUT في هذا المقرر هو Update/Replace.
In the response line HTTP/1.1 200 OK, what is OK?
في سطر الرد HTTP/1.1 200 OK، ما هو OK؟
C. Slide 12 labels 200 as the status code and OK as the status message. HTTP/1.1 is the protocol version.
C. الشريحة 12 تسمّي 200 رمز الحالة (status code) وOK رسالة الحالة (status message). وHTTP/1.1 هو إصدار البروتوكول.
A response carries Content-Length: 500. What does 500 measure?
رد يحتوي على Content-Length: 500. ماذا يقيس الرقم 500؟
B. Content-Length is the body length in bytes, which is how the client knows where the body ends. A counts the headers too. C is wrong because a character can be more than one byte in UTF-8.
B. الـ Content-Length هو طول الـ body بالبايت، وبه يعرف العميل أين ينتهي الـ body. والخيار A يعدّ الـ headers أيضاً. والخيار C خطأ لأن الحرف الواحد قد يشغل أكثر من بايت في UTF-8.
In GET /index.html HTTP/1.1 with Host: localhost, which matches slide 11?
في GET /index.html HTTP/1.1 مع Host: localhost، أي إجابة تطابق الشريحة 11؟
D, straight from the slide's table. GET is the method, HTTP/1.1 is the version, and Accept: text/html tells the server what media type the client wants.
D، مأخوذة مباشرة من جدول الشريحة. GET هو الـ method، وHTTP/1.1 هو الإصدار، وAccept: text/html يخبر الخادم بنوع الوسائط الذي يريده العميل.
Which method is idempotent but not safe?
أي method يكون idempotent لكنه ليس safe؟
B. PUT changes state (not safe), but repeating it leaves the same state (idempotent). DELETE is the same. GET is both safe and idempotent; POST is neither. D reverses the relation: every safe method is idempotent, not the other way round.
B. الـ PUT يغيّر الحالة (فهو ليس safe)، لكن تكراره يترك الحالة نفسها (فهو idempotent). والـ DELETE مثله. أما GET فهو safe وidempotent معاً، وPOST ليس أياً منهما. والخيار D يعكس العلاقة: كل method آمن هو idempotent، وليس العكس.