Search code examples
asp.net-web-apirestful-architecturehttp-verbs

HTTP Verbs, WebAPI


I would like to know the usage scenario of POST vs PUT in a WebAPI . I know the basic concepts that POST is for creating resource and PUT is for updating resource but not able to fully understand why we need a PUT over a POST.

I have 2 WebAPI methods which creates/updates data to my SQL store 1. CreateUser(UserDto) 2. UpdateUser(UserDto)

UserDto contains userId, username and email.

I can use POST for both CreateUser and UpdateUser methods which creates and updates user to my store.

Then what is the real advantage of using POST for CreateUser and PUT for updateuser? Is it just a standard/convention?

Thank you


Solution

  • POST always creates something new. PUT updates a existing thing. It is a convention.

    You should have:

    POST /users : to create a new user. The payload should not include the ID

    PUT /user/(id) : to replace a user DTO with the data in the payload. Again, the payload should not contain an user id

    PATCH /user/(id): to update specific members of the user, but the id.

    It is a design convention, like software design patterns, to make it easy to communicate and understand by whoever has to consume the API.