Tutte le specialitàTecnologia · Colloquio di lavoro

REST API

Le domande su REST sembrano semplici, ma le risposte buone mostrano un vero giudizio di progettazione. Impara a spiegare verbi, status code e retry sicuri in un inglese breve e chiaro.

Parole → espressioni → frasi → parlato

Prova SpeakRole ↗
Colloquio di lavoroREST API
V

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

0:18
Domanda dell’intervistatore

REST API

Chi intervista usa le domande su REST per capire come progetti un'interfaccia che altri sviluppatori useranno per anni. Ascolta la semantica HTTP corretta, ma anche il buon senso pratico: errori utili al client, retry sicuri e modifiche che non rompono gli utenti esistenti.

Rispondi con una risorsa concreta, come gli ordini, e continua a usarla. Un esempio come una POST su slash orders è più facile da dire a voce e da seguire di una regola astratta. Cita un compromesso ogni volta che enunci una regola, perché REST ha molti casi in cui «dipende».

  • ✓Parole → espressioni → frasi → parlato
  • ✓Senso della risposta
  • ✓Lingua

Domande comuni e risposte di esempio

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

Come progetti una REST API per una risorsa come gli ordini e quando usi PUT, PATCH e POST?

Che cosa vuole sentire l'intervistatore. Sostantivi negli URL, metodi HTTP con il significato giusto, assenza di stato (statelessness) e la differenza tra sostituire e aggiornare in parte.

Risposta di esempio in inglese da esercitare ad alta voce

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?

Quali status code HTTP usi e come restituisci gli errori?

Che cosa vuole sentire l'intervistatore. Le famiglie giuste e i codici specifici (201, 204, 400, 401, 403, 404, 409, 429), più un unico corpo di errore coerente.

Risposta di esempio in inglese da esercitare ad alta voce

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?

Che cos'è l'idempotenza e perché è importante per una API?

Che cosa vuole sentire l'intervistatore. Una definizione precisa, quali metodi sono idempotenti e come rendere sicura da ripetere una POST, per esempio con una idempotency key.

Risposta di esempio in inglese da esercitare ad alta voce

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?

Come implementi la paginazione e che problema ha la paginazione con offset?

Che cosa vuole sentire l'intervistatore. Paginazione con offset contro paginazione con cursore, il costo sulle tabelle grandi, la coerenza quando i dati cambiano e una dimensione massima della pagina.

Risposta di esempio in inglese da esercitare ad alta voce

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?

Come metti in sicurezza una REST API?

Che cosa vuole sentire l'intervistatore. HTTPS, autenticazione contro autorizzazione, accesso basato su token come OAuth 2.0 e controlli dei permessi per ogni oggetto.

Risposta di esempio in inglese da esercitare ad alta voce

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.

Come pronunciare i termini chiave

REST
rèst
idempotent
ai-dem-pòu-tent
endpoint
ènd-pòint
payload
pèi-loud
pagination
pa-gi-nèi-shon
OAuth
òu-oth (il «th» è quello di «think»)
JSON
gèi-son

Frasi utili per rispondere

  • I would model this as a resource.Modellerei questo come una risorsa.
  • The client can safely retry this request.Il client può ripetere questa richiesta senza rischi.
  • I would return a 409, because…Restituirei un 409, perché…
  • The response includes a link to the next page.La risposta include un link alla pagina successiva.
  • We keep the API backward compatible by…Manteniamo l'API retrocompatibile grazie a…
  • The trade-off is simplicity against flexibility.Il compromesso è tra semplicità e flessibilità.
  • The server never trusts the client input.Il server non si fida mai dell'input del client.

Errori comuni da evitare

  • Mettere verbi negli URL, come "getOrders" o "deleteUser". Usa sostantivi per le risorse e lascia che sia il metodo HTTP a descrivere l'azione. Puoi citare la rara eccezione per azioni come "cancel".
  • Restituire 200 per ogni risposta e mettere l'errore nel corpo. Client, proxy e monitoraggio si basano sullo status code, quindi usa quello corretto.
  • Confondere 401 e 403. 401 significa «non so chi sei», 403 significa «so chi sei, ma non puoi farlo».
  • Dire che PUT e POST sono la stessa cosa. Spiega che PUT sostituisce una risorsa nota e si può ripetere senza rischi, mentre POST di solito crea qualcosa e non è idempotente.
  • Evitare la parola "idempotent" o pronunciarla in modo diverso ogni volta. In italiano «idempotènte» ha l'accento sulla penultima sillaba, in inglese cade su po. Dilla lentamente una volta: eye-dem-POH-tent. È una parola chiave di questo argomento.

Domande frequenti

REST è la stessa cosa di una API HTTP con JSON?

Non esattamente. REST è uno stile architetturale con idee come le risorse, l'assenza di stato e un'interfaccia uniforme. Molte API «REST» ne seguono solo una parte, e puoi dirlo con onestà: "we build resource-oriented HTTP APIs with JSON".

Come parlo del versionamento delle API?

Di' che cerchi di evitare le modifiche che rompono la compatibilità: aggiungi campi invece di rimuoverli o rinominarli. Quando una rottura è inevitabile, introduci una nuova versione, più spesso nell'URL o in un header, mantieni la vecchia per un periodo concordato e avvisi i client per tempo.

Devo citare OpenAPI o Swagger?

Sì, se li hai usati. Un contratto scritto in OpenAPI aiuta i team a mettersi d'accordo prima di programmare, produce documentazione e può servire a generare codice client e test. Di' come il tuo team teneva allineati contratto e codice.

Leggere una risposta non è la stessa cosa che dirla. Esercita ogni domanda ad alta voce, poi confronta il significato e la lingua della tua risposta.

Prova SpeakRole ↗

Altri argomenti in quest’area