Web Development APIs and REST 2 — Questions and Answers
Question 1: Which HTTP status code indicates that a resource was successfully created?
- 200 OK
- 201 Created (Correct answer)
- 204 No Content
- 202 Accepted
Correct answer: 201 Created
201 Created is the standard response when a POST request successfully creates a new resource.
Question 2: What does the HTTP OPTIONS method primarily do in a REST API?
- Deletes a resource
- Returns allowed methods for a resource (Correct answer)
- Updates a resource partially
- Retrieves resource metadata only
Correct answer: Returns allowed methods for a resource
OPTIONS returns the HTTP methods that the server supports for the specified URL, commonly used in CORS preflight requests.
Question 3: In REST, what does 'statelessness' mean?
- The server never stores any data
- Each request must contain all information needed to process it (Correct answer)
- Clients cannot store session data
- The API never returns errors
Correct answer: Each request must contain all information needed to process it
Statelessness means the server does not store client session state; every request must be self-contained with all necessary context.
Question 4: Which of the following best describes HATEOAS?
- A security protocol for REST APIs
- A constraint where API responses include links to related actions (Correct answer)
- A method for compressing API responses
- An authentication standard for REST
Correct answer: A constraint where API responses include links to related actions
HATEOAS (Hypermedia as the Engine of Application State) means responses include hypermedia links that guide clients to possible next actions.
Question 5: What is the purpose of the 'ETag' HTTP header in REST APIs?
- To encrypt the response body
- To identify the content type
- To enable conditional requests and caching validation (Correct answer)
- To specify the API version
Correct answer: To enable conditional requests and caching validation
ETags are identifiers for a specific version of a resource, allowing clients to make conditional requests and avoid downloading unchanged content.
Question 6: Which HTTP method should be used to partially update a resource in a REST API?
- PUT
- POST
- PATCH (Correct answer)
- UPDATE
Correct answer: PATCH
PATCH is used for partial updates, sending only the fields that need to change, while PUT replaces the entire resource.
Question 7: What does a 429 HTTP status code indicate?
- The request payload is too large
- The client has sent too many requests in a given time (Correct answer)
- The server is temporarily unavailable
- Authentication credentials are invalid
Correct answer: The client has sent too many requests in a given time
429 Too Many Requests indicates that the client has exceeded the API's rate limit and should slow down its requests.
Which HTTP status code indicates that a resource was successfully created?