Junior — Middle
Is it possible to use the HTTP PUT method for initializing and creating a new resource?
sobes.tech AI
Answer from AI
Yes, the HTTP PUT method can be used for initializing and creating a new resource. In REST architecture, PUT is typically used to create or replace a resource at a known URI.
Features of using PUT for creation:
- The client sends a PUT request to a specific URI, which will be the address of the new resource.
- If the resource at this URI does not exist, the server creates it with the provided content.
- If the resource exists, it is replaced with the new content.
Unlike POST, which creates a resource on the server and usually does not guarantee the URI of the new resource, PUT requires the client to know the URI in advance.
Example:
PUT /users/123 HTTP/1.1
Content-Type: application/json
{
"name": "Ivan",
"email": "ivan@example.com"
}
If a user with ID 123 does not exist, it will be created; if it exists — it will be updated.