CSI RESTful API & Middleware Design 2 — Questions and Answers
Question 1: Which HTTP status code should a REST API return when a client submits a request with a malformed JSON body?
- 400 Bad Request (Correct answer)
- 422 Unprocessable Entity
- 415 Unsupported Media Type
- 500 Internal Server Error
Correct answer: 400 Bad Request
400 Bad Request is returned when the server cannot parse the request due to malformed syntax, including invalid JSON.
Question 2: In REST API design, what is the purpose of the ETag response header?
- To specify the encoding format of the response body
- To provide a version identifier for cache validation (Correct answer)
- To authenticate the client's identity on subsequent requests
- To indicate the API version being used
Correct answer: To provide a version identifier for cache validation
ETag provides a hash or version token that clients send in If-None-Match headers to enable conditional requests and cache revalidation.
Question 3: A middleware component in an API gateway intercepts all inbound requests. Which design pattern does this exemplify?
- Observer pattern
- Chain of Responsibility pattern (Correct answer)
- Facade pattern
- Decorator pattern
Correct answer: Chain of Responsibility pattern
Chain of Responsibility passes requests along a chain of handlers (middleware), where each can process or forward the request.
Question 4: When designing a REST API for paginated collections, which approach best aligns with REST constraints?
- Use POST with page parameters in the body
- Include pagination metadata in response headers like Link (Correct answer)
- Always return the complete dataset and let clients filter
- Store cursor state server-side in session
Correct answer: Include pagination metadata in response headers like Link
Using Link headers (RFC 5988) keeps pagination controls hypermedia-driven and stateless, consistent with REST principles.
Question 5: Which HTTP method is idempotent but NOT safe according to REST semantics?
- GET
- HEAD
- PUT (Correct answer)
- OPTIONS
Correct answer: PUT
PUT is idempotent (multiple identical calls produce the same result) but not safe because it modifies server state.
Question 6: What is the primary role of a message broker middleware like RabbitMQ in a system integration scenario?
- Providing direct synchronous HTTP bridging between services
- Decoupling producers and consumers through asynchronous message queuing (Correct answer)
- Enforcing authentication across all microservices
- Compressing API payloads to reduce bandwidth
Correct answer: Decoupling producers and consumers through asynchronous message queuing
Message brokers decouple services by allowing producers to publish messages independently of when consumers process them.
Question 7: In an API versioning strategy, what is a key disadvantage of embedding the version in the URL path (e.g., /v1/users)?
- It violates the HTTP specification
- It makes caching impossible for CDNs
- It breaks the REST principle that URIs should identify resources, not versions (Correct answer)
- It requires clients to always use HTTPS
Correct answer: It breaks the REST principle that URIs should identify resources, not versions
URI-based versioning violates REST purity because the same resource should have a stable URI regardless of representation version.
Which HTTP status code should a REST API return when a client submits a request with a malformed JSON body?