I've heard that in REST world, POST
is recommended to create an entry, while PUT
is recommended to update an entry.
First, I'd like a confirmation of this statement.
Then, using this assumptions, let's say I have a @POST method to create a user and a @PUT method to update a user (with a @QueryParam to pass the user ID).
What is the correct way to pass parameters to POST
and PUT
?
Is @FormParam
appropriate for @PUT
? Or should I pass a JSON
in the body?
Should I pass parameters the same way for both @POST
and @PUT
or a different way?
Or should I use POST
for both?
Edit: This question initially showed an example that did not work for me, but it was because my testing tool was doing it wrong. It works with POSTMAN now.
Yes, with REST, you typically use the following:
POST
of the element list resource to add an elementPUT
of the element resource to completely update an elementPATCH
of the element resource to partially update an elementSince what you must send corresponds to the state of the resource, you have to provide it within the request body.
The two bodies (for adding and updating) is similar but there are some differences. For example, if you expect the RESTful service to autogenerate some fields, you don't have to provide corresponding ones.
Here are sample requests:
POST /contacts
{
"lastName": "my last name",
"firstName": "my first name",
}
(corresponding response status code: 201 - Created)
PUT /contacts/contactid
{
"lastName": "my last name",
"firstName": "my first name",
}
(corresponding response status code: 204 - No content)
You can notice that JSON isn't the only format you can use. XML, YAML, and so on could also be used.
I think that the following link could give you some hints:
Hope it helps you, Thierry