Geert-Jan
Geert-Jan

Reputation: 18925

should I use PUT to create a resource with a known id (e.g.: email)

I believe to have read some time ago that creating a resource when the id is known (e.g.: email) should be done using a PUT on that resource.

E.g.: PUT /user/[email protected]

is this correct?

Upvotes: 5

Views: 808

Answers (3)

Pedro Werneck
Pedro Werneck

Reputation: 41928

Yes, it's correct to use PUT to create a resource with a known URI. PUT asks the server to replace the resource at the target URI with the resource representation in the payload, so you have to know the target URI. However, keep in mind that PUT requires a complete representation, so if you're creating or updating a resource with an incomplete representation, you should use POST.

Upvotes: 2

Timothy Shields
Timothy Shields

Reputation: 79611

Yes, it is correct to use

PUT resource/{id} --> 204 No Content

when the id of the resource is being specified by the client and the operation is idempotent. The operation is idempotent if doing it two or more times in a row has the same effect as doing it once.

If you use POST, you usually do not provide a client identifier. Instead the server chooses its own identifier and informs the client of the created resource's location by sending a 201 Created response with a Location header.

POST resource     --> 201 Created
                      Location: /resource/7

Upvotes: 4

Boris Kraportov
Boris Kraportov

Reputation: 76

I think what more correctly to do it throught POST on new url and create if thid id does not exists. Because PUT method is idempotent (e. g. if you send one request many time - result would be same).

Upvotes: 0

Related Questions