0
0
Rest APIprogramming~10 mins

PUT for full replacement in Rest API - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - PUT for full replacement
Client sends PUT request
Server receives request
Check if resource exists
Send 201 Created
Send 200 OK
Client receives response
The client sends a PUT request to replace a resource fully. The server checks if the resource exists, replaces it if yes, or creates it if no, then responds accordingly.
Execution Sample
Rest API
PUT /users/123 HTTP/1.1
Host: example.com
Content-Type: application/json

{"name": "Alice", "age": 30}
Client sends a PUT request to replace user 123's data with new name and age.
Execution Table
StepActionResource Exists?Server OperationResponse CodeResponse Description
1Receive PUT request for /users/123UnknownCheck resource existence--
2Check if user 123 existsYesReplace entire user 123 data with new JSON--
3Save new user dataYesOverwrite old data--
4Send response to clientYesSend 200 OK200Resource replaced successfully
5Client receives responseYes---
6If resource did not existNoCreate new user 123 with given data--
7Send response to clientNoSend 201 Created201Resource created successfully
💡 Execution stops after sending 200 OK if resource existed or 201 Created if resource was newly created.
Variable Tracker
VariableStartAfter Step 2After Step 3After Step 4
resource_existsUnknownYes or NoYes or NoYes or No
resource_dataOld data or NoneOld data or NoneNew data from PUT bodyNew data from PUT body
response_codeNoneNoneNone200 or 201
Key Moments - 2 Insights
Why does the server send 200 OK sometimes and 201 Created other times?
The server sends 200 OK when it replaces an existing resource (see execution_table row 4). It sends 201 Created when the resource did not exist and is created new (see row 7).
Does PUT add new fields to the existing resource or replace it completely?
PUT replaces the entire resource with the new data provided. It does not merge or add fields. This is shown in execution_table row 3 where old data is overwritten.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what response code is sent if the resource exists?
A201
B200
C404
D500
💡 Hint
Check execution_table row 4 where resource_exists is Yes and response_code is 200.
At which step does the server decide to create a new resource?
AStep 4
BStep 2
CStep 6
DStep 1
💡 Hint
Look at execution_table row 6 where resource_exists is No and server creates new resource.
If the client sends partial data, what happens to the missing fields in the resource?
AThey are deleted
BThey are merged with new data
CThey stay unchanged
DServer returns error
💡 Hint
Recall that PUT replaces the entire resource (see key_moments and execution_table row 3). Missing fields are removed.
Concept Snapshot
PUT request fully replaces a resource at the given URL.
If resource exists, server overwrites it and returns 200 OK.
If resource does not exist, server creates it and returns 201 Created.
PUT requires full data; partial updates are not merged.
Use PUT for complete replacement, not partial changes.
Full Transcript
This visual trace shows how a PUT request works for full replacement in REST APIs. The client sends a PUT request with new data for a resource. The server first checks if the resource exists. If yes, it replaces the entire resource with the new data and sends back a 200 OK response. If the resource does not exist, the server creates it with the new data and sends a 201 Created response. The key point is that PUT replaces the whole resource, so any missing fields in the new data will remove those fields from the resource. This is different from PATCH, which only updates parts. The execution table walks through each step, showing the server's decision and response. The variable tracker shows how resource existence, data, and response code change during the process. The key moments clarify common confusions about response codes and data replacement. The quiz tests understanding of these steps and outcomes. Overall, PUT is used when you want to fully replace or create a resource at a specific URL.