Introduction
PUT is used to completely replace a resource on a server with new data. It updates everything about that resource.
PUT is used to completely replace a resource on a server with new data. It updates everything about that resource.
PUT /resource/{id} HTTP/1.1
Host: example.com
Content-Type: application/json
{
"field1": "new value",
"field2": "new value"
}PUT /users/123 HTTP/1.1 Host: api.example.com Content-Type: application/json { "name": "Alice", "email": "alice@example.com" }
PUT /products/456 HTTP/1.1 Host: api.example.com Content-Type: application/json { "name": "New Product", "price": 19.99, "stock": 100 }
This Python program sends a PUT request to replace the post with ID 1 on a test API. It updates the title and body fully.
import requests url = 'https://jsonplaceholder.typicode.com/posts/1' new_data = { "userId": 1, "id": 1, "title": "Updated Title", "body": "This is the updated body of the post." } response = requests.put(url, json=new_data) print('Status code:', response.status_code) print('Response JSON:', response.json())
PUT replaces the entire resource, so missing fields may be removed.
Use PATCH if you want to update only some fields without replacing everything.
Always send the full resource data with PUT to avoid losing information.
PUT fully replaces a resource at the given URL.
It can create the resource if it does not exist.
Use PUT when you want to update all fields of a resource at once.
/users/123 with new data in JSON format?/users/42 initially having data {"name": "Alice", "age": 30}, what will be the resource data after this PUT request?PUT /users/42
{"name": "Bob"}
PUT /items/10
{"price": 100}
/users/77 with new data {"name": "Eve", "age": 28}. The resource may or may not exist. Which is the best approach using PUT to ensure the resource is created if missing or fully replaced if exists?Pass the test and this pattern moves unknown