Bird
Raised Fist0
Rest APIprogramming~10 mins

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

Choose your learning style10 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.

Practice

(1/5)
1. What does the HTTP PUT method do when used on a REST API resource URL?
easy
A. Retrieves the resource data from the URL
B. Partially updates the resource fields at the URL
C. Fully replaces the resource at the URL with the provided data
D. Deletes the resource at the URL

Solution

  1. Step 1: Understand PUT method purpose

    PUT is designed to replace the entire resource at the given URL with the data sent in the request.
  2. Step 2: Compare with other methods

    Unlike PATCH which updates partially, PUT replaces fully. GET retrieves, DELETE removes.
  3. Final Answer:

    Fully replaces the resource at the URL with the provided data -> Option C
  4. Quick Check:

    PUT = full replacement [OK]
Hint: PUT replaces entire resource, not partial update [OK]
Common Mistakes:
  • Confusing PUT with PATCH partial update
  • Thinking PUT deletes resource
  • Assuming PUT only retrieves data
2. Which of the following is the correct HTTP method and usage to fully replace a resource at URL /users/123 with new data in JSON format?
easy
A. POST /users/123 with JSON body
B. PUT /users/123 with JSON body
C. PATCH /users/123 with JSON body
D. GET /users/123 with JSON body

Solution

  1. Step 1: Identify method for full replacement

    PUT is the HTTP method used to fully replace a resource at a specific URL.
  2. Step 2: Match method with URL and body

    PUT /users/123 with JSON body replaces the user data fully. POST is for creation, PATCH partial update, GET retrieves.
  3. Final Answer:

    PUT /users/123 with JSON body -> Option B
  4. Quick Check:

    PUT + URL + JSON = full replace [OK]
Hint: Use PUT with URL and full JSON body to replace resource [OK]
Common Mistakes:
  • Using POST instead of PUT for replacement
  • Using PATCH for full replacement
  • Sending body with GET request
3. Given a REST API with a user resource at /users/42 initially having data {"name": "Alice", "age": 30}, what will be the resource data after this PUT request?
PUT /users/42
{"name": "Bob"}
medium
A. {"name": "Bob"}
B. {"name": "Bob", "age": 30}
C. {"age": 30}
D. Resource deleted

Solution

  1. Step 1: Understand PUT replaces entire resource

    The PUT request replaces the whole resource data with the new JSON body, removing any fields not included.
  2. Step 2: Apply replacement to given data

    Original data had name and age, but PUT body only has name "Bob", so age is removed.
  3. Final Answer:

    {"name": "Bob"} -> Option A
  4. Quick Check:

    PUT replaces all fields, missing fields removed [OK]
Hint: PUT replaces all fields; missing fields are removed [OK]
Common Mistakes:
  • Assuming PUT merges fields like PATCH
  • Expecting old fields to remain if not sent
  • Thinking PUT deletes resource
4. You wrote this PUT request to update a resource:
PUT /items/10
{"price": 100}

But after sending, the resource lost all other fields except price. How can you fix this to keep other fields intact?
medium
A. Use GET method instead
B. Add missing fields to the PUT JSON body
C. Send an empty body with PUT
D. Change PUT to PATCH to update partially

Solution

  1. Step 1: Identify cause of lost fields

    PUT replaces the entire resource, so missing fields in body are removed.
  2. Step 2: Choose method to update partially

    PATCH updates only specified fields, keeping others intact.
  3. Final Answer:

    Change PUT to PATCH to update partially -> Option D
  4. Quick Check:

    PATCH = partial update, keeps other fields [OK]
Hint: Use PATCH for partial update, PUT replaces all [OK]
Common Mistakes:
  • Trying to fix by adding missing fields manually
  • Sending empty body with PUT
  • Using GET to update resource
5. You want to update a user resource at /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?
hard
A. Send a PUT request with full user data to /users/77
B. Send a POST request to /users with partial data
C. Send a PATCH request to /users/77 with partial data
D. Send a GET request to /users/77 before updating

Solution

  1. Step 1: Understand PUT behavior for create or replace

    PUT can create the resource if it does not exist, or fully replace it if it does.
  2. Step 2: Apply PUT with full data to target URL

    Sending PUT with full user data to /users/77 ensures creation or full replacement.
  3. Final Answer:

    Send a PUT request with full user data to /users/77 -> Option A
  4. Quick Check:

    PUT = create or replace resource fully [OK]
Hint: PUT creates or replaces resource at URL with full data [OK]
Common Mistakes:
  • Using POST which creates new resource with new ID
  • Using PATCH which only updates existing resource partially
  • Using GET before update unnecessarily
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard