PUT vs POST while creating a resource

The most asked question and one where I have seen the most inconsistency and interchangeable use in REST APIs is whether to use the HTTP method PUT or POST while designing a REST API to create a User entity.
Usually, I have seen suggestions such as use POST to create and PUT to update, or use POST to create and update.
Though, it depends on how you want to model your APIs and many more factors, the most simplest approach that can get you started and help you to distinguish and decide which HTTP method to use is to know whether –
The Entity’s id is provided by the client, with the API path similar to – /v1/users/{user_id}
OR
The Entity’s id is created on the server, with the API path similar to – /v1/users

If the id is provided by client, you can go for PUT, that is, PUT /v1/users/{user_id}
Else, go for POST, that is POST /v1/users

Note: For a more detailed explanation, refer to this stackoverflow thread

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s