The PUT Method: Replacing a Product
How an administrator replaces a product completely: a PUT request with the full data, code 200, and how PUT differs from PATCH.
About the product
Hi! Changing a price is clear. But what if you need to update the whole product: both the name and the price? Today we look at the second way to change a product — the PUT method — and compare it with PATCH.
Got it! So how does it work?
Kadri will explain everything: Kadri is already waiting for you in the meeting room. We only cover the positive scenario: everything goes as it should.
Hi! You already know PATCH: it changes part of the data. To replace a product completely, HTTP has the PUT method.
Hi Kadri! How is PUT different from PATCH?
PUT means “replace completely”. The client puts the product in the address and sends all its data in the body: both the name and the price. The server replaces the product with it entirely. Here is an example for the product with id 5:
Request
PUT /api/v1/products/5
{
"name": "Rye bread, 400 g",
"price": 1.99
}- the method — what to do.
PUTmeans “replace completely” - the address — what to do it with.
/api/v1/products/5is the product withid5 - the request body — the complete product data: the name and the price. Even if only one field changed, everything is sent
And what does the server answer to PUT?
The server replaces the product data in the database and answers with the code 200 OK. In the response body it returns the whole product. The id stays the same: PUT changes the content, not the product number.
Response
200 OK
{
"id": 5,
"name": "Rye bread, 400 g",
"price": 1.99
}- the status code — how it went.
200 OKmeans “success” - the response body — the whole product:
id, name and price
Note: 200 is common, but not the only option. Depending on the implementation, the server may answer, for example, 204 No Content with no body, or 201 Created if there was no product with this id yet and PUT created it.
Let’s check you’ve got it. What is the main difference between PUT and PATCH?
Let’s see it in practice!
Here is the BB Food admin panel. Choose a product, change the name and the price and press “Save product”. Note that the form has both fields, because PUT sends the whole product.
Application under test
Scenario. Positive: the administrator replaces a product’s data
The server is waiting for requests. Press Save product to make the client send a PUT request.
The animation is slowed down: in reality the whole trip takes a few dozen milliseconds.
| id | name | price |
|---|
Enter a name and a price — a number above zero.
There are no products left in the catalogue.
Done! The server returned 200 OK and the whole product. The whole row was updated in the table: both the name and the price, while the id stayed the same.
The administrator changed only the price in the form, but pressed “Save product”. What will the client send in the PUT body?
So what should I check here?
Check three things. First: the response code is 200 OK, and the body has the whole product with the new data. Second: in the database table the name and the price were updated, and the id did not change. Third: a separate GET /api/v1/products request returns the product with the new name and price, so customers will see them too.
Quick reference
The PUT method
PUT /api/v1/products/5 means “replace completely”. The request body has all the data of the object, and the server replaces the old data with it.
Code 200 OK
A success response. The server returns the whole product in the body, already with the new data. The id itself doesn’t change on replacement.
PUT and PATCH
PUT sends all fields, even if only one changed. PATCH sends only the fields that change.
What a tester checks
The response code is 200, and the body has the product with the new data. In the database the name and the price are updated, and the id is the same. The new data is visible in the admin panel and in the catalogue.