REST API资源超限及课程注册超员场景下的HTTP响应码咨询
Great question—let's break this down into the general case and your specific course enrollment scenario to keep it clear.
General Scenario: Resource Reaches Maximum Limit
When a resource hits its predefined maximum capacity, the best HTTP status code to use is 409 Conflict. This code is explicitly meant for situations where the request can't be completed because it clashes with the current state of the target resource. A course filling up to its student cap is a perfect example: your request to create a new enrollment directly conflicts with the course's "fully enrolled" state.
Don't mix this up with other codes that don't fit:
403 Forbidden: This is for permission issues (e.g., the user isn't allowed to enroll in any courses at all), not resource capacity limits.413 Payload Too Large: Only applies when the request body itself is too big, not when the resource is full.429 Too Many Requests: Used for rate limiting (too many requests in a short time), not when a specific resource's quota is exhausted.
Your Course Enrollment Endpoint
For your POST http://my-api/class/learn-rest/enrollment endpoint, when a user tries to create the 6th enrollment (with a 5-student cap):
- Return 409 Conflict as the status code.
- Pair it with a clear, actionable message in the response body (JSON is standard for REST APIs) so the client understands exactly why the request failed. For example:
{ "error": "Enrollment failed: The 'learn-rest' course has reached its maximum capacity of 5 students." }
This gives developers (and end-users, if the client surfaces the message) a straightforward explanation instead of just a vague status code.
内容的提问来源于stack exchange,提问作者americansoldier__

