What HTTP status code should an API return when a POST request creates a resource successfully, and what header should accompany it?
- A.201 Created, with a 'Location' header pointing to the URL of the newly created resource (e.g., Location: /api/users/456)
- B.200 OK with the created resource in the response body
- C.202 Accepted with a polling URL for the asynchronous creation operation
- D.204 No Content to indicate the resource was created without a response body
Why A is correct
201 Created is the correct semantics for successful POST resource creation. The Location header tells the client where to find the new resource - clients can then GET it without parsing the response body. 202 Accepted is for asynchronous operations where creation is queued. 204 No Content means success with no body - correct for DELETE/PUT updates with no content to return, not for creation. Using 200 for creation works but loses the semantic of 'something new was created here.'
Know someone studying for Web App Fundamentals? Send them this one.