Skip to lesson

HTTP and REST interview questions

A review of this module. Skip it if you feel confident; the next module does not need it.

Fix the mistakes

The slips that cost marks in this module. Spot each one, then check the fix.

Interviewers rarely ask for a bare definition. They ask what happens when you type a URL, why a retried POST is dangerous, or why curl works when the browser blocks the call, and they expect an example.

What happens when you type a URL; HTTP vs HTTPS

HTTPS is HTTP sent inside TLS, which adds encryption, integrity and authentication; the HTTP messages themselves are the same.

The classic opener: tell the whole story in order, from the typed address to the page on the screen. The picture shows the trip; the example is a model answer.

The trip of a URL in seven steps. One, the browser parses the URL. Two, DNS turns the name into an IP address. Three, TCP connects and TLS encrypts. Four, the browser sends the HTTP request, for example GET /products/1. Five, the server routes it to a handler and the database.
The browser sends, the server answers, the browser draws the page.

Example 1 · Typing a URL, step by step

Text
https://shop.example/products/1 1. Parse     scheme https, host shop.example, path /products/1 2. DNS       shop.example -> 203.0.113.10 (browser, OS, then resolver caches) 3. TCP       connect to 203.0.113.10:443 (three-way handshake) 4. TLS       certificate for shop.example checked; session keys agreed 5. Request   GET /products/1 HTTP/1.1, Host, Accept, cookies 6. Server    load balancer -> app server -> router -> handler -> service -> database 7. Response  200 OK, Content-Type text/html, Cache-Control, body 8. Render    parse HTML, fetch CSS/JS/images (same connection or new ones), run scripts
Sample output
Interview answer (about 40 seconds):
"The browser parses the URL and resolves shop.example to an IP with DNS. It opens
a TCP connection to port 443 and does a TLS handshake, which checks the certificate
and agrees on encryption keys. Then it sends GET /products/1 with headers such as
Host and cookies. On the server a load balancer passes it to the app, whose router
calls the handler, the service and the database. The browser gets 200 with HTML,
renders it and fetches the CSS, scripts and images it needs."

What this code does

  • Interviewers look for the order (DNS, TCP, TLS, HTTP, server, render) more than for every detail.
  • Mentioning your own server's path (router, handler, service, database) shows you know the server side, not only the browser.

Encryption means nobody on the Wi-Fi can read the messages. Integrity means nobody can change them on the way. Authentication means the certificate proves the server really is that domain.

Methods, idempotency and status codes

An idempotent method leaves the server in the same state however many times it is repeated, as GET, PUT and DELETE do; POST is not idempotent.

Safe methods do not change data: GET, HEAD, OPTIONS. Idempotent methods give the same server state when repeated: the safe ones plus PUT and DELETE. POST is neither, so a retried POST can create two orders; that is why payment APIs accept an Idempotency-Key header. PATCH is not idempotent in general ("add 1 to stock"), though a PATCH that sets fields is.

Know the status families and the common codes cold: 200, 201 (+ Location), 204, 301/302/304, 400, 401 (not authenticated), 403 (authenticated but not allowed), 404, 405, 409, 415, 422, 429 (too many requests), 500, 502, 503.

Cookies, sessions, tokens and CORS

A preflight is the OPTIONS request a browser sends before a non-simple cross-origin call, to ask the API whether the real request is allowed.

HTTP forgets every request, so login state must travel with each one. Cookies, sessions, tokens and CORS decide how, and the preflight below is the part interviews ask about most.

Example 2 · A CORS preflight, as the browser sends it

Text
OPTIONS /api/v1/orders HTTP/1.1Host: api.shop.exampleOrigin: https://app.shop.exampleAccess-Control-Request-Method: POSTAccess-Control-Request-Headers: authorization, content-type
Output
HTTP/1.1 204 No Content
Access-Control-Allow-Origin: https://app.shop.example
Access-Control-Allow-Methods: GET, POST
Access-Control-Allow-Headers: authorization, content-type
Access-Control-Max-Age: 600

(The browser now sends the real POST. Without these headers it blocks the
 response from the page's JavaScript; the server may still have run a simple request.)

What this code does

  • The JSON body and the Authorization header make the request non-simple, so the browser asks first with OPTIONS.
  • Max-Age lets the browser cache the answer for 10 minutes; the allowed origin is one exact origin, not *, because the request carries credentials.

With sessions the server stores the state and the browser sends a session id in a cookie (HttpOnly, Secure, SameSite). With tokens (often a signed JWT) the client sends Authorization: Bearer ... and the server checks the signature without a lookup; a stolen token stays valid until it expires, so tokens are short-lived.

CORS is a browser rule, not server security: a page from https://app.example may call https://api.example only if the API's response allows that origin (Access-Control-Allow-Origin). curl and Postman ignore CORS completely.

REST design, versioning, pagination; Jackson and HttpClient

Design questions expect the conventions plus a reason: nouns and methods, 201 + Location, Problem Details errors, versioning in the path (/api/v1) with additive changes allowed and breaking ones in v2. For pagination, compare offset paging (page, size; easy, can jump to page 7, but slow for deep pages and rows shift when data changes) with cursor/keyset paging (after=lastId; fast and stable, but no page jumps).

Java questions check that you used the tools well: create one HttpClient and one Jackson mapper and reuse them (both are thread-safe once built), set a connect timeout on the client and a request timeout per request, check statusCode() yourself because HttpClient does not throw on 4xx/5xx, and read JSON into records.

Weak answers and better ones

Interviewers notice the one-line answers that stop too early. Each weak answer below becomes a strong one with a single extra fact:

  • "HTTPS is secure HTTP" becomes encryption, integrity and server authentication by TLS.
  • "PUT updates, POST creates" becomes the same plus idempotency and what a retry does.
  • "CORS protects the API" becomes it protects users in browsers; curl ignores it.
  • "JWT is better than sessions" becomes the trade-off: no lookup against revocation.
  • "HttpClient throws on 404" becomes it does not; check statusCode().

Finish up

Mark complete & continue

Next: Mini project: command-line client for a public API