Scrivio Interview library
DesignIntermediate5 min read · updated 12 November 2026

When would you return 200, 201, 202 and 204 from POST endpoints? Give an example for each.

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.

Step throughChoosing the success code for a POST
  1. Did the POST create a new resource?With its own URL
    Yes201 Created with LocationNoNext question
  2. Is the work finished when we respond?Or queued for later
    YesNext questionNo202 Accepted with status link
  3. Is there a useful result to return?A calculation, a summary
    Yes200 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.

Java
@PostMapping("/orders")
ResponseEntity<OrderDto> create(@Valid @RequestBody CreateOrder req) {
OrderDto created = orders.create(req);
URI location = ServletUriComponentsBuilder.fromCurrentRequest()
.path("/{id}").buildAndExpand(created.id()).toUri();
return ResponseEntity.created(location).body(created);
}
// HTTP/1.1 201 Created
// Location: https://api.example.com/orders/42
// {"id":42,"status":"NEW",...}

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.

Java
@PostMapping("/reports")
ResponseEntity<JobStatus> requestReport(@Valid @RequestBody ReportRequest req) {
Job job = jobs.enqueue(req); // stored, processed by a worker
return ResponseEntity.accepted()
.location(URI.create("/reports/jobs/" + job.id()))
.body(new JobStatus(job.id(), "QUEUED"));
}
// HTTP/1.1 202 Accepted
// Location: /reports/jobs/7
// GET /reports/jobs/7 -> {"status":"RUNNING"} ... later {"status":"DONE","result":"/reports/991"}
Step throughA 202 flow from request to result
ClientReports APIWorker
POST /reports
202 Accepted, Location: /reports/jobs/7
job 7 queued
GET /reports/jobs/7
RUNNING
job 7 done, report 991
GET /reports/jobs/7
DONE, result /reports/991

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.

Java
@PostMapping("/quotes/calculate")
QuoteDto calculate(@Valid @RequestBody QuoteRequest req) {
return pricing.quote(req); // nothing stored
}
// HTTP/1.1 200 OK
// {"subtotal":120.00,"tax":21.60,"total":141.60}

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.

Java
@PostMapping("/orders/{id}/archive")
@ResponseStatus(HttpStatus.NO_CONTENT)
void archive(@PathVariable long id) {
orders.archive(id);
}
// HTTP/1.1 204 No Content (no 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.

POST success codes at a glance
CodeMeaningExampleMust include
201 CreatedA new resource existsPOST /ordersLocation header
202 AcceptedQueued, finished laterPOST /reportsLink to a status resource
200 OKResult returned, nothing createdPOST /quotes/calculateThe result in the body
204 No ContentDone, nothing to returnPOST /orders/42/archiveNo 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.

What to say in the interviewAbout 90 seconds aloud

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.
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/