Right HTTP method to create or update RESTful?

I am creating a RESTful web service that allows me to import documents by name. I would import the document using the following path:

/documents/frequently-asked-questions 

If the document does not exist yet, it will create a new one; otherwise, it will simply overwrite the existing document.

I have a question whether this is the wrong endpoint for a RESTful service. Usually I use POST to create and PUT to update. Here he does not know in advance whether this document exists. If this is reasonable, then what is the best HTTP method? If this is not correct, then what is the best approach?

+8
design rest web-services
source share
1 answer

HTTP 1.1 Specification for POST:

9.5 POST

The POST method is used to request that the source server accept the object enclosed in the request as a new subordinate resource identified by the Request-URI in the query string.

And for PUT:

9.6 PUT

The PUT method requests that the private object be stored in the supplied Request-URI.

Given that PUT is idempotent and POST is not, PUT seems to be the logical choice here for your creation and updating.

Source: http://www.w3.org/Protocols/rfc2616/rfc2616-sec9.html#sec9.1.2

+15
source

All Articles