How to rename resources in idempotent mode?

I implemented an API that renames the company as follows:

PUT /companies/A
{
  "name": "B"
}

returns HTTP 301with a header Locationindicating the new URI company: /companies/B.

How to do this idempotent operation with If-Matchand without headers ?

  • Without a header If-Match: if a user tries to rename a company that HTTP 404does not exist, I expect the server to return , but I can’t do it because then the legitimate renaming operations will not be idempotent (they will be returned 301the first time, but 404on subsequent calls). This is problematic because I want clients to be able to distinguish between failed renames (the company does not exist) compared to the renaming that has already taken place.

  • With the heading If-Match: if the company ETagdepends on the name of the company, subsequent renaming operations will not be performed because the precondition is no longer fulfilled. Again, this makes the operation seem to have failed, when in fact this has already taken place.

+4
source share
3 answers

The PUT operation succeeds and should return 200 or 201. Subsequent requests for the same resource should return 301 with the appropriate response authority indicating the URI of the new resource.

404 should only be for resources that really cannot be found, i.e. companies that do not exist and never have.

protocol, idempotence , . , . , idempotence , , 2xx (, 301).

, , , .

+1

, , , - , .

, HTTP () idempotent, POST PUT.

: PUT - "create-or-replace", RFC 2616 ( )

PUT , Request-URI.

RFC 7231 ( , , ), :

PUT    ,    . PUT    , GET    ,    200 (OK).

, , PUT .

PS. , PUT, - , , . , , , POST .

+5

, 3xx. PUT , 2xx. 301 , , .

, , MOVE, MOVE: -)

0

All Articles