Tüm uzmanlıklarTeknoloji · İş görüşmesi

REST API

REST soruları basit görünür ama iyi cevaplar gerçek bir tasarım muhakemesini gösterir. Fiilleri, durum kodlarını ve güvenli yeniden denemeleri açık ve kısa İngilizceyle anlatmayı öğrenin.

Kelimeler → ifadeler → cümleler → konuşma

SpeakRole’i dene ↗
İş görüşmesiREST API
V

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

0:18
Mülakat sorusu

REST API

Mülakatçılar REST sorularıyla, başka geliştiricilerin yıllarca kullanacağı bir arayüzü nasıl tasarladığınızı görmek ister. Doğru HTTP anlamlarını dinlerler; ayrıca pratik sağduyuya da bakarlar: istemciye yardımcı olan hatalar, güvenli yeniden denemeler ve mevcut kullanıcıları bozmayan değişiklikler.

Siparişler gibi tek bir somut kaynakla cevap verin ve onu kullanmaya devam edin. "slash orders"a bir POST gibi bir örneği yüksek sesle söylemek ve takip etmek, soyut bir kuraldan daha kolaydır. Bir kural verdiğinizde bir ödünleşimden (trade-off) de söz edin, çünkü REST'te “duruma göre değişir” diyeceğiniz çok durum vardır.

  • ✓Kelimeler → ifadeler → cümleler → konuşma
  • ✓Yanıtın anlamı
  • ✓Dil

Sık sorulan mülakat soruları ve örnek cevaplar

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

Siparişler gibi bir kaynak için REST API'yi nasıl tasarlarsınız ve PUT, PATCH ile POST'u ne zaman kullanırsınız?

Mülakatçı ne duymak ister. URL'lerde isimler, doğru anlama sahip HTTP metotları, durumsuzluk (statelessness) ve bir kaynağı değiştirmek (replace) ile kısmen güncellemek arasındaki fark.

Yüksek sesle çalışmak için İngilizce örnek cevap

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?

Hangi HTTP durum kodlarını kullanırsınız ve hataları nasıl döndürürsünüz?

Mülakatçı ne duymak ister. Doğru kod aileleri ve belirli kodlar (201, 204, 400, 401, 403, 404, 409, 429), ayrıca tek tutarlı bir hata gövdesi.

Yüksek sesle çalışmak için İngilizce örnek cevap

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 nedir ve bir API için neden önemlidir?

Mülakatçı ne duymak ister. Kesin bir tanım, hangi metotların idempotent olduğu ve bir POST'u yeniden denemek için güvenli hâle getirmek; örneğin bir idempotency key ile.

Yüksek sesle çalışmak için İngilizce örnek cevap

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?

Sayfalamayı (pagination) nasıl uygularsınız ve offset sayfalamanın sorunu nedir?

Mülakatçı ne duymak ister. Offset ve cursor sayfalama, büyük tablolardaki maliyet, veri değiştiğinde tutarlılık ve en yüksek sayfa boyutu.

Yüksek sesle çalışmak için İngilizce örnek cevap

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?

Bir REST API'yi nasıl güvenli hâle getirirsiniz?

Mülakatçı ne duymak ister. HTTPS, kimlik doğrulama ile yetkilendirme, OAuth 2.0 gibi token tabanlı erişim ve her nesne için izin kontrolleri.

Yüksek sesle çalışmak için İngilizce örnek cevap

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.

Anahtar terimler nasıl telaffuz edilir

REST
rest (“dinlenme” anlamındaki sözcük gibi okunur)
idempotent
ay-dem-PO-tınt
endpoint
END-poynt
payload
PEY-lod
pagination
pec-ı-NEY-şın
OAuth
O-ot
JSON
CEY-sın

Cevap vermek için kullanışlı kalıplar

  • I would model this as a resource.Bunu bir kaynak (resource) olarak modellerdim.
  • The client can safely retry this request.İstemci bu isteği güvenle yeniden deneyebilir.
  • I would return a 409, because…Şu nedenle 409 döndürürdüm:…
  • The response includes a link to the next page.Yanıt, bir sonraki sayfaya bir bağlantı içerir.
  • We keep the API backward compatible by…API'yi geriye dönük uyumlu tutuyoruz; bunu şöyle yapıyoruz:…
  • The trade-off is simplicity against flexibility.Ödünleşim (trade-off), sadelik ile esneklik arasında.
  • The server never trusts the client input.Sunucu, istemciden gelen girdiye asla güvenmez.

Sık yapılan hatalar

  • URL'lere "getOrders" ya da "deleteUser" gibi fiiller koymak. Kaynaklar için isim kullanın ve eylemi HTTP metodunun anlatmasına izin verin. "cancel" gibi eylemler için nadir istisnadan söz edebilirsiniz.
  • Her yanıt için 200 döndürüp hatayı gövdeye koymak. İstemciler, proxy'ler ve izleme sistemleri durum koduna dayanır; bu yüzden doğru olanı kullanın.
  • 401 ile 403'ü karıştırmak. 401 “kim olduğunuzu bilmiyorum”, 403 ise “kim olduğunuzu biliyorum ama bunu yapamazsınız” demektir.
  • PUT ile POST'un aynı olduğunu söylemek. PUT'un bilinen bir kaynağı değiştirdiğini ve güvenle tekrarlanabildiğini, POST'un ise genellikle bir şey oluşturduğunu ve idempotent olmadığını açıklayın.
  • "idempotent" sözcüğünden kaçınmak ya da her seferinde farklı okumak; örneğin “i-dem-po-TENT”. Bir kez yavaşça söyleyin: ay-dem-PO-tınt. Bu, konunun anahtar sözcüğüdür.

Sıkça sorulan sorular

REST, JSON kullanan bir HTTP API ile aynı şey midir?

Tam olarak değil. REST; kaynaklar, durumsuzluk ve tekdüze arayüz (uniform interface) gibi fikirleri olan bir mimari stildir. Birçok “REST” API bunun yalnızca bir kısmını izler ve bunu dürüstçe söyleyebilirsiniz: "we build resource-oriented HTTP APIs with JSON".

API versiyonlamadan nasıl söz ederim?

Bozucu değişikliklerden (breaking changes) kaçınmaya çalıştığınızı söyleyin: alanları kaldırmak ya da yeniden adlandırmak yerine yenilerini eklersiniz. Bozulma kaçınılmazsa yeni bir sürüm çıkarırsınız; çoğunlukla URL'de ya da bir başlıkta (header). Eskisini üzerinde anlaşılan bir süre desteklersiniz ve istemcileri erkenden bilgilendirirsiniz.

OpenAPI ya da Swagger'dan söz etmeli miyim?

Kullandıysanız evet. OpenAPI'de yazılı bir sözleşme, ekiplerin kod yazmadan önce anlaşmasına yardımcı olur, dokümantasyon üretir ve istemci kodu ile testlerin üretilmesine yardım edebilir. Ekibinizin sözleşmeyi ve kodu nasıl senkron tuttuğunu anlatın.

Bir cevabı okumak ile onu söylemek aynı şey değildir. Her soruyu yüksek sesle çalışın, ardından kendi cevabınızın anlamını ve dilini karşılaştırın.

SpeakRole’i dene ↗

Bu alandaki diğer konular