Junior — Middle
Can you name HTTP methods that do not guarantee the same result on repeated calls?
sobes.tech AI
Answer from AI
HTTP methods that do not guarantee the same result on repeated calls are called non-idempotent. These methods include:
- POST — typically used to create resources; each call can create a new resource, so the result changes.
- PATCH — partial update of a resource; can change the state with each call.
- DELETE — deletes a resource; the first call deletes the resource, subsequent calls may return an error or another result.
In contrast, methods like GET, PUT, HEAD, OPTIONS, TRACE are considered idempotent, meaning repeated calls produce the same result (in terms of resource state).
Example:
// POST request will create a new object each time
fetch('/api/items', { method: 'POST', body: JSON.stringify({name: 'item'}) })
// PUT request will update the resource; repeated calls with the same data will not change the state
fetch('/api/items/1', { method: 'PUT', body: JSON.stringify({name: 'updated'}) })