Skip to content

Commit 1368a3f

Browse files
authored
Merge pull request #737 from zalando/tfrauenstein-patch-1
Improve guidance for return code usage for (robust) create.
2 parents d6b3ef3 + 259a927 commit 1368a3f

File tree

2 files changed

+36
-25
lines changed

2 files changed

+36
-25
lines changed

chapters/http-requests.adoc

Lines changed: 25 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -90,16 +90,20 @@ implicitly creating the resource before updating
9090
* on successful {PUT} requests, the server will *replace the entire resource*
9191
addressed by the URL with the representation passed in the payload (subsequent
9292
reads will deliver the same payload, plus possibly server-generated fields like `modified_at`)
93-
* successful {PUT} requests will usually generate {200} or {204} (if the
94-
resource was updated – with or without actual content returned), and {201} (if
95-
the resource was created)
96-
97-
*Important:* It is good practice to prefer {POST} over {PUT} for creation of
98-
(at least top-level) resources. This leaves the resource identifier management under
99-
control of the service and not the client, and focuses {PUT} on its usage for updates.
100-
However, in situations where all resource attributes including the identifier
93+
* successful {PUT} requests return {200} or {204} (if the resource was updated -
94+
with or without returning the resource), {201} (if the resource was created)
95+
or {202} (if accepted and processed asynchronously).
96+
97+
The updated/created resource may be returned as response payload. We recommend,
98+
to not use it per default, but if the resource is enriched with server-generated
99+
fields like `version_number`. You may also support client side steering (see <<181>>).
100+
101+
*Important:* It is good practice to keep the resource identifier management under
102+
control of the service provider and not the client, and, hence, to prefer {POST} for
103+
creation of (at least top-level) resources, and focus {PUT} on its usage for updates.
104+
However, in situations where the identifier and all resource attributes
101105
are under control of the client as input for the resource creation you should use
102-
{PUT} and pass the resource identifier via the URL path.
106+
{PUT} and pass the resource identifier as URL path parameter.
103107
Putting the same resource twice is required to be <<idempotent>> and to result
104108
in the same single resource instance (see <<149>>) without data duplication in case of repetition.
105109

@@ -122,10 +126,9 @@ by the URL"_.
122126

123127
* on a successful {POST} request, the server will create one or multiple new
124128
resources and provide their URI/URLs in the response
125-
* successful {POST} requests will usually generate {200} (if resources have
126-
been updated), {201} (if resources have been created), {202} (if the request
127-
was accepted but has not been finished yet), and exceptionally {204} with
128-
{Location} header (if the actual resource is not returned).
129+
* successful {POST} requests return {200} or {204} (if the resource was updated -
130+
with or without returning the resource), {201} (if the resource was created)
131+
or {202} (if accepted and processed asynchronously).
129132

130133
*Note:* By using {POST} to create resources the resource ID must not be passed as
131134
request input date by the client, but created and maintained by the service and
@@ -159,8 +162,8 @@ collection is challenging
159162
instances
160163
* on successful {PATCH} requests, the server will update parts of the resource
161164
addressed by the URL as defined by the change request in the payload
162-
* successful {PATCH} requests will usually generate {200} or {204} (if
163-
resources have been updated with or without updated content returned)
165+
* successful {PATCH} requests return {200} or {204} (if the resource was updated -
166+
with or without returning the resource).
164167

165168
*Note:* since implementing {PATCH} correctly is a bit tricky, we strongly suggest
166169
to choose one and only one of the following patterns per endpoint (unless
@@ -206,8 +209,8 @@ described as _"please delete the resource identified by the URL"_.
206209
collection resources, as this would imply deleting the entire collection.
207210
* {DELETE} request can be applied to multiple resources at once using query
208211
parameters on the collection resource (see <<delete-with-query-params>>).
209-
* successful {DELETE} requests will usually generate {200} (if the deleted
210-
resource is returned) or {204} (if no content is returned).
212+
* successful {DELETE} requests return {200} or {204} (if the resource was deleted -
213+
with or without returning the resource).
211214
* failed {DELETE} requests will usually generate {404} (if the resource cannot
212215
be found) or {410} (if the resource was already deleted before).
213216

@@ -374,6 +377,11 @@ If you mainly aim to support safe retries, we suggest to apply <<182,
374377
conditional key>> and <<231,secondary key>> pattern before the <<230,
375378
Idempotency Key>> pattern.
376379

380+
Note, like for {PUT}, successful {POST} or {PATCH} returns {200} or {204} (if the resource
381+
was updated - with or without returning the resource), or {201} (if resource was created).
382+
Hence, clients can differentiate successful robust repetition from resource created
383+
server activity of idempotent {POST}.
384+
377385

378386
[#231]
379387
== {Should} use secondary key for idempotent `POST` design

chapters/http-status-codes-and-errors.adoc

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -84,22 +84,23 @@ a clear description of the HTTP status code in the response.
8484
|=======================================================================
8585
|Code |Meaning |Methods
8686
|[[status-code-200]]{200}|
87-
OK - this is the standard success response
87+
OK - this is the most general success response and used,
88+
if the more specific codes below are not applicable.
8889
|{ALL}
8990

9091
|[[status-code-201]]{201}|
91-
Created - Returned on successful entity creation. You are
92-
free to return either an empty response or the created resource in conjunction
93-
with the Location header. (More details found in the <<standard-headers>>.)
94-
_Always_ set the Location header.
92+
Created - Returned on successful resource creation.
93+
{201} is returned with or without response payload (unlike {200} / {204}).
94+
We recommend to additionally return the created resource URL via the `Location`
95+
response header (see <<standard-headers>>).
9596
|{POST}, {PUT}
9697

9798
|[[status-code-202]]{202}|
9899
Accepted - The request was successful and will be processed asynchronously.
99100
|{POST}, {PUT}, {PATCH}, {DELETE}
100101

101102
|[[status-code-204]]{204}|
102-
No content - There is no response body.
103+
No content - Returned instead of {200}, if no response payload is returned.
103104
|{PUT}, {PATCH}, {DELETE}
104105

105106
|[[status-code-207]]{207}|
@@ -175,8 +176,10 @@ Request timeout - the server times out waiting for the resource.
175176

176177
|[[status-code-409]]{409}|
177178
Conflict - request cannot be completed due to conflict with the current state of the target resource.
178-
For instance, in situations where two clients try to create the same resource or if there are
179-
concurrent, conflicting updates.
179+
For example, you may get a {409} response when updating a resource that is older than the existing
180+
one on the server, resulting in a version control conflict.
181+
Hint, you should not return {409}, but {200} or {204} in case of successful robust creation of resources
182+
(via {PUT} or {POST}), if the resource already exists.
180183
|{POST}, {PUT}, {PATCH}, {DELETE}
181184

182185
|[[status-code-410]]{410}|

0 commit comments

Comments
 (0)