كل التخصصاتالتقنية · مقابلة عمل

REST API

تبدو أسئلة REST بسيطة، لكن الإجابات الجيدة تُظهر حسًّا حقيقيًا بالتصميم. تعلّم أن تشرح الأفعال (verbs) ورموز الحالة وإعادة المحاولة الآمنة بإنجليزية قصيرة وواضحة.

كلمات → عبارات → جمل → تحدث

جرّب SpeakRole ↗
مقابلة عملREST API
V

How do you design a REST API that stays backward compatible?

0:18
سؤال المقابل

REST API

يستخدم المحاورون أسئلة REST ليروا كيف تصمّم واجهة سيستخدمها مطورون آخرون لسنوات. يستمعون إلى دلالات HTTP الصحيحة، وكذلك إلى الحس العملي: أخطاء تفيد العميل، وإعادة محاولة آمنة، وتغييرات لا تكسر المستخدمين الحاليين.

أجب بمورد (resource) واحد محدد، مثل orders، وواصل استخدامه. مثال مثل POST إلى "slash orders" أسهل في النطق وفي المتابعة من قاعدة مجردة. اذكر مفاضلة كلما ذكرت قاعدة، لأن في REST حالات كثيرة يكون فيها الجواب «هذا يعتمد».

  • ✓كلمات → عبارات → جمل → تحدث
  • ✓معنى الإجابة
  • ✓اللغة

أكثر الأسئلة تكرارًا ونماذج الإجابات

How do you design a REST API for a resource like orders, and when do you use PUT, PATCH and POST?

كيف تصمّم REST API لمورد مثل orders، ومتى تستخدم PUT وPATCH وPOST؟

ما الذي يريد المحاور سماعه. أسماء في الروابط، وطرق HTTP بمعانيها الصحيحة، وعدم حفظ الحالة (statelessness)، والفرق بين الاستبدال والتحديث الجزئي.

نموذج إجابة بالإنجليزية للتدرّب عليه بصوت عالٍ

I model the resource as a noun, so I have slash orders for the collection and slash orders slash 123 for one order. The HTTP method carries the action. GET reads. POST creates a new order, and I return 201 with a Location header. PUT replaces the whole order, so sending the same request twice gives the same result. PATCH changes only some fields. DELETE removes the order. The server stays stateless: each request carries everything needed, including the authentication token. For actions that do not fit a resource, like cancelling an order, I can use a sub-resource, and I stay consistent.

Which HTTP status codes do you use, and how do you return errors?

ما رموز حالة HTTP التي تستخدمها، وكيف تُرجع الأخطاء؟

ما الذي يريد المحاور سماعه. العائلات الصحيحة ورموز محددة (201 و204 و400 و401 و403 و404 و409 و429)، مع بنية خطأ واحدة ثابتة.

نموذج إجابة بالإنجليزية للتدرّب عليه بصوت عالٍ

I use the status code families on purpose. 2xx means success: 200 for a normal response, 201 for a created resource and 204 when there is nothing to return. 4xx means the client made a mistake: 400 for a bad request, 401 when the user is not authenticated, 403 when the user is known but not allowed, 404 when the resource is missing, 409 for a conflict, and 429 when the client sends too many requests. 5xx means our server failed. I return the same JSON error format everywhere: a code, a clear message and details for validation errors. I never show stack traces.

What is idempotency, and why does it matter for an API?

ما الـ idempotency، ولماذا تهم الـ API؟

ما الذي يريد المحاور سماعه. تعريف دقيق، وأي الطرق idempotent، وكيف تجعل POST آمنًا لإعادة المحاولة، مثلًا بمفتاح idempotency.

نموذج إجابة بالإنجليزية للتدرّب عليه بصوت عالٍ

An operation is idempotent when repeating it leaves the server in the same state as doing it once. GET, PUT and DELETE are idempotent by definition, while POST is not. This matters because networks fail. A client sends a payment request, gets a timeout and does not know if it worked. If it retries and the request is not idempotent, the customer may pay twice. For such cases I support an idempotency key: the client sends a unique key in a header, the server stores the key with the result, and a repeated request returns the saved result instead of doing the work again.

How do you implement pagination, and what is wrong with offset pagination?

كيف تنفّذ الترقيم الصفحي (pagination)، وما العيب في الترقيم بالإزاحة (offset)؟

ما الذي يريد المحاور سماعه. الترقيم بالإزاحة مقابل الترقيم بالمؤشر (cursor)، والكلفة في الجداول الكبيرة، والاتساق عند تغيّر البيانات، وحجم أقصى للصفحة.

نموذج إجابة بالإنجليزية للتدرّب عليه بصوت عالٍ

The simplest way is offset pagination: the client sends a limit and an offset. It is easy to use and allows jumping to a page. But it has two problems. On big tables, the database still has to skip all the earlier rows, so late pages become slow. And if rows are added or deleted while the client is reading, items can be skipped or repeated. For large or fast-changing data, I prefer cursor pagination. The client sends the cursor from the last item, usually based on a key such as the creation time and the ID, and the server returns the next items after it. In both cases I set a maximum page size.

How do you secure a REST API?

كيف تؤمّن REST API؟

ما الذي يريد المحاور سماعه. HTTPS، والمصادقة مقابل التفويض، والوصول بالرموز (tokens) مثل OAuth 2.0، وفحص الصلاحيات لكل كائن على حدة.

نموذج إجابة بالإنجليزية للتدرّب عليه بصوت عالٍ

First, everything runs over HTTPS. For authentication, I usually use tokens. With OAuth 2.0, a client gets a short-lived access token, often a JWT, and sends it in the Authorization header. A refresh token can get a new one. For server-to-server calls, an API key can identify the caller, but it is not a replacement for user permissions. After authentication, I check authorisation on the server for every request, including whether this user may access this specific object. A missing check there is a common vulnerability. I also validate the input, apply rate limiting and avoid putting tokens in URLs, because URLs end up in logs.

كيف تنطق المصطلحات الأساسية

REST
رِسْتْ
idempotent
أَيْدِمْبُوتِنْت
endpoint
إِنْدْبُويْنْت
payload
بَيْلُود
pagination
بَادْجِنَيْشِن
OAuth
أُو-أُوثْ
JSON
جَيْسُن

عبارات مفيدة للإجابة

  • I would model this as a resource.كنت سأنمذج هذا بوصفه مورِدًا (resource).
  • The client can safely retry this request.يستطيع العميل إعادة هذا الطلب بأمان.
  • I would return a 409, because…كنت سأُرجع 409 لأن…
  • The response includes a link to the next page.تتضمن الاستجابة رابطًا إلى الصفحة التالية.
  • We keep the API backward compatible by…نُبقي الـ API متوافقًا مع الإصدارات السابقة عن طريق…
  • The trade-off is simplicity against flexibility.المفاضلة هنا بين البساطة والمرونة.
  • The server never trusts the client input.لا يثق الخادم أبدًا بمدخلات العميل.

أخطاء شائعة

  • وضع أفعال في الروابط، مثل "getOrders" أو "deleteUser". استخدم أسماء للموارد ودع طريقة HTTP تصف الإجراء. ويمكنك ذكر الاستثناء النادر لإجراءات مثل "cancel".
  • إرجاع 200 لكل استجابة ووضع الخطأ في الجسم. تعتمد العملاء والوكلاء (proxies) وأنظمة المراقبة على رمز الحالة، فاستخدم الرمز الصحيح.
  • الخلط بين 401 و403. 401 تعني «لا أعرف من أنت»، و403 تعني «أعرف من أنت، لكن لا يحق لك فعل هذا».
  • القول إن PUT وPOST متماثلتان. اشرح أن PUT تستبدل مورِدًا معروفًا ويمكن تكرارها بأمان، أما POST فتُنشئ عادةً شيئًا جديدًا وليست idempotent.
  • تجنّب كلمة "idempotent" أو نطقها بشكل مختلف في كل مرة. قلها ببطء مرة واحدة: eye-dem-POH-tent، أي «أَيْدِمْبُوتِنْت». إنها كلمة أساسية في هذا الموضوع.

الأسئلة الشائعة

هل REST هو نفسه HTTP API بصيغة JSON؟

ليس تمامًا. REST أسلوب معماري له أفكار مثل الموارد وعدم حفظ الحالة وواجهة موحدة. كثير من واجهات «REST» لا تتبع منه إلا جزءًا، ويمكنك قول ذلك بصدق: "we build resource-oriented HTTP APIs with JSON".

كيف أتحدث عن إصدارات الـ API (versioning)؟

قل إنك تحاول تجنب التغييرات التي تكسر التوافق: تضيف حقولًا بدل حذفها أو إعادة تسميتها. وحين لا مفرّ من الكسر، تُصدر نسخة جديدة، غالبًا في الرابط أو في ترويسة، وتدعم القديمة فترة متفقًا عليها، وتُبلغ العملاء مبكرًا.

هل أذكر OpenAPI أو Swagger؟

نعم، إذا استخدمتهما. العقد المكتوب بـ OpenAPI يساعد الفرق على الاتفاق قبل البرمجة، ويولّد توثيقًا، ويمكن أن يساعد في توليد كود العميل والاختبارات. قل كيف أبقى فريقك العقد والكود متطابقين.

قراءة الإجابة ليست مثل قولها. تدرّب على كل سؤال بصوت عالٍ، ثم قارن معنى إجابتك ولغتها بالنموذج.

جرّب SpeakRole ↗

موضوعات أخرى في هذا المجال