Put VS Post - what to use for web and REST?

I often hear this discussed. When to use PUT and POST verbs in HTTP? how do they differ? and are they not very closely related?

When to use POST

  • When you need to create a resource
  • Sending the same post request twice will create two instances
  • No specific resource is specified in the url (like /books)

When to use PUT

  • Creating or updating a specific resource
  • Sending the same put request twice creates and then updates the resource (nothing happens as it is the same values given for the resource)
  • A specific resource is targeted in the URL (like /books/1)

Something important to remember is that, you do not need to support both! Whatever you intended to do is the verb you should use. So if your API only create comments but does not support editing - then go with POST. If however the user provides the key for the resource they are creating - or for some reason has it - use put. Meaning if they are going to make an update.