Quick revision
The short answer
Return 201 Created when the POST creates a resource, with a Location header pointing to it, for example POST /orders. Return 202 Accepted when the work is queued and finished later, with a link to a status resource, for example POST /reports. Return 200 OK when the POST computes and returns a result without creating anything, for example POST /quotes/calculate. Return 204 No Content when the action succeeds and there is nothing useful to send back, for example POST /orders/42/archive. Whatever we choose, apply it consistently and document it in the API spec.
What to remember
In this answer, we saw that each success code answers a question for the client. 201 Created means a resource was created and the Location header says where, as with POST /orders. 202 Accepted means the work is queued, with a status link to poll, as with report generation. 200 OK returns a computed result without creating anything, as with a price calculation, and 204 No Content says the action succeeded with nothing to return, as with archiving. We keep the choice consistent through a guideline and the OpenAPI spec.
Your turn
Answer it aloud as you would in the interview, or type your answer below. Then show one part at a time and compare.
Return 201 Created when the POST creates a resource, with a Location header pointing to it, for example POST /orders. Return 202 Accepted when the work is queued and finished later, with a link to a status resource, for example POST /reports. Return 200 OK when the POST computes and returns a result without creating anything, for example POST /quotes/calculate. Return 204 No Content when the action succeeds and there is nothing useful to send back, for example POST /orders/42/archive. Whatever we choose, apply it consistently and document it in the API spec.
Why this question is asked
POST is the most flexible HTTP method, so it ends up doing many different things: creating, triggering, calculating, searching. Each of those deserves a different success code, and clients rely on the difference, for example to know whether they should poll for a result. Interviewers ask this to see whether we pick status codes by meaning rather than habit. In this answer, we will recap what a success code tells the client, go through 201, 202, 200 and 204 with a Spring example for each, and finish with how to keep the choice consistent.
1A Quick Recap: What a Success Code Tells the Client
All 2xx codes mean the request succeeded, but each answers a different follow-up question for the client. Was something created, and where is it? Is the work done, or only accepted? Is there a body to read? A client that gets 201 knows to read the Location header. A client that gets 202 knows the outcome is not known yet and it must check back. A client that gets 204 knows not to parse a body.
Using 200 for everything technically works, but it throws that information away, and clients then have to inspect the body to understand what happened.
- Did the POST create a new resource?With its own URLYes201 Created with LocationNoNext question
- Is the work finished when we respond?Or queued for laterYesNext questionNo202 Accepted with status link
- Is there a useful result to return?A calculation, a summaryYes200 OK with the resultNo204 No Content
3 steps in order. Play them, or step through them one at a time.
2201 Created: A New Resource Exists
When a POST creates something that has its own URL, we answer 201 and put that URL in the Location header. Including the created representation in the body saves the client a GET.
Notice that 201 is about the resource, not the method. A PUT that creates a resource at a client-chosen URL can return 201 too.
3202 Accepted: The Work Is Queued
Some requests start work that takes longer than a client should wait: generating a large report, importing a file, running a payment through a slow external system. We validate the request, store a job, and answer 202 with a link to a status resource the client can poll.
8 messages in order. Play them, or step through them one at a time.
Notice that 202 makes no promise that the work will succeed; it only says the request was accepted for processing. The status resource is where the client learns the outcome, and when it is done, it can link to the created report. Webhooks or server-sent events can replace polling, but a status URL is the simplest contract.
4200 OK: A Result, but Nothing Created
Some POSTs are really function calls: they take input, compute something and return it, without storing anything the client can later address. Price quotes, tax calculations, validation checks and complex searches with a body fall here. We use POST because the input is large or structured, but nothing is created, so 201 would be wrong and 200 with the result is right.
Notice that the plain return value is enough here: Spring sends 200 by default, so no ResponseEntity is needed.
5204 No Content: Done, Nothing to Say
Action endpoints often have no useful response: archive an order, resend a confirmation email, mark notifications as read. If the action is complete and the client needs nothing back, 204 says exactly that, and clients know not to parse a body.
If the client would benefit from the new state, such as the order's updated status, returning 200 with the representation is also fine. The choice should follow what clients need, not a rule that actions must return 204.
6Being Consistent and Documenting It
The codes only help clients if they are predictable. A team guideline can state the rule from our flow: creation returns 201 with Location, queued work returns 202 with a status link, calculations return 200, and actions without a result return 204. The OpenAPI spec lists the exact success code for each operation, so generated clients and documentation agree with the server, and a lint rule can flag a POST that creates resources but documents only 200.
| Code | Meaning | Example | Must include |
|---|---|---|---|
| 201 Created | A new resource exists | POST /orders | Location header |
| 202 Accepted | Queued, finished later | POST /reports | Link to a status resource |
| 200 OK | Result returned, nothing created | POST /quotes/calculate | The result in the body |
| 204 No Content | Done, nothing to return | POST /orders/42/archive | No body |
7Follow-up questions
What should a retried POST return if the order was already created?
With an idempotency key, the server recognises the retry and returns the same response as the first time, typically the original 201 with the same Location, so the client cannot tell and does not create a duplicate. Without a key, the second request may legitimately create a second order, or hit a unique constraint and return 409.
Can a 202 job end in failure?
Yes. 202 only says the request was accepted. The status resource reports the final state, such as FAILED with a problem detail explaining why, and the client must handle that case.
8Conclusion
In this answer, we saw that each success code answers a question for the client. 201 Created means a resource was created and the Location header says where, as with POST /orders. 202 Accepted means the work is queued, with a status link to poll, as with report generation. 200 OK returns a computed result without creating anything, as with a price calculation, and 204 No Content says the action succeeded with nothing to return, as with archiving. We keep the choice consistent through a guideline and the OpenAPI spec.
Open with
I pick the code by what actually happened. If the POST creates a resource with its own URL, like POST /orders, it's 201 Created with a Location header pointing to the new order, and usually the created representation in the body.
Then, if they want more
In Spring that's ResponseEntity.created(location).body(dto). If the work takes too long to do in the request, like generating a big report or importing a file, I validate, store a job and return 202 Accepted with a link to a status resource the client can poll. 202 only means accepted, so the job can still fail, and the status resource reports that.
If the POST is really a calculation, like a price quote with a structured input, nothing is created, so 201 would be misleading. I return 200 with the result, which is Spring's default anyway.
And if it's an action with nothing useful to return, like archiving an order or resending an email, it's 204 No Content, for example with @ResponseStatus(NO_CONTENT) on a void method. If clients would benefit from the new state, 200 with the representation is fine too.
The important part is consistency: the rule goes into our API guideline, every operation lists its success code in the OpenAPI spec, and a lint rule can catch a creating POST that only documents 200.
About this answer
- Last updated
- 12 November 2026
- Checked
- Its structure and format are checked automatically. Use it as a carefully prepared study note: where a detail matters, confirm it against the documentation below. What the checks cover
- Where the question comes from
- Written by Scrivio for practice. It is not taken from an interview.
- Official documentation
- Spring Framework reference, Spring Web MVC
Report an issue with this answer
Copy the line below and send it to the author with what you found. It carries the address of this page.
Issue with "When would you return 200, 201, 202 and 204 from POST endpoints? Give an example for each.": https://scrivio-interview-library.netlify.app/spring-boot/rest-controllers/7728e91e5e/