Senior
What HTTP methods do you know? What is the difference between POST and PUT?
sobes.tech AI
Answer from AI
Main HTTP methods:
- GET — retrieve a resource
- POST — create a resource or perform an action
- PUT — replace a resource entirely
- PATCH — partially update a resource
- DELETE — delete a resource
- HEAD — get only the headers of a resource
- OPTIONS — find out supported methods
Differences between POST and PUT:
- POST is usually used to create new resources. POST requests are not idempotent — repeated requests can create multiple resources.
- PUT is used to replace or create a resource at a known URL. PUT is idempotent — repeated requests with the same data will not change the result.
Example:
- POST /users — creates a new user with a new ID
- PUT /users/123 — updates or creates a user with ID 123
Thus, POST is for creation with a server that assigns an identifier, PUT is for updating or creating at a specific address.