deejo
deejo

Reputation: 451

HTTP status to return when POST is called multiple times?

I have a POST end-point which creates/inserts a new record in database.

POST /payments/cards will add/save a payment card in DB and return the added card information in response when 200 OK. Now, if we invoke the same end-point again, its updating the existing payment card in DB which shouldn't happen.

So, I am adding logic check if the payment card already exists so that we don't update it.

What is the HTTP status to be returned in this scenario?

Upvotes: 0

Views: 964

Answers (1)

Rarblack
Rarblack

Reputation: 4664

For a POST response, use 201 if a new record is created, otherwise use 200 or maybe 409 instead.I think that it is 409 Conflict which is the most appropriate, however, seldom seen in the wild of course:

The request could not be completed due to a conflict with the current state of the resource. This code is only allowed in situations where it is expected that the user might be able to resolve the conflict and resubmit the request. The response body SHOULD include enough information for the user to recognize the source of the conflict. Ideally, the response entity would include enough information for the user or user agent to fix the problem; however, that might not be possible and is not required.

Conflicts are most likely to occur in response to a PUT request. For example, if versioning were being used and the entity being PUT included changes to a resource which conflict with those made by an earlier (third-party) request, the server might use the 409 response to indicate that it can't complete the request. In this case, the response entity would likely contain a list of the differences between the two versions in a format defined by the response Content-Type.

Upvotes: 1

Related Questions