The PATCH Method: Changing a Price
How an administrator changes part of a product’s data: a PATCH request with the price only, code 200, and an updated column in the database.
About the product
Hi! We’ve learned to add products. But more often a catalogue just needs a small fix: for example, changing a price. Today we look at how an administrator changes the price of a product.
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! To change an existing product, HTTP has the PATCH and PUT methods. Let’s start with PATCH: it changes only part of the data.
Hi Kadri! What does a PATCH request look like?
PATCH means “change partially”. The client puts in the address which product to change, and sends in the body only what needs to change. For example, a new price for the product with id 3:
Request
PATCH /api/v1/products/3
{
"price": 1.29
}- the method — what to do.
PATCHmeans “change part of the data” - the address — what to do it with.
/api/v1/products/3is the product withid3 - the request body — only the field that changes. The name doesn’t need to be sent: it stays as it was
And what does the server answer to PATCH?
The server updates only the field that was sent in the database and answers with the code 200 OK. In the response body the server returns the whole product, already with the new price, so the client can show up-to-date data at once.
Response
200 OK
{
"id": 3,
"name": "Milk 2.5%, 1 l",
"price": 1.29
}- the status code — how it went.
200 OKmeans “success” - the response body — the whole product:
id, name and the new 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.
Let’s check you’ve got it. An administrator wants to change only the price of a product, and the name should stay. What will the client send in the body of the PATCH request?
Let’s see it in practice!
Here is the BB Food admin panel. Choose a product, enter a new price and press “Change price”. Follow the path of the request: client → server → database → server → client.
Application under test
Scenario. Positive: the administrator changes a product’s price
The server is waiting for requests. Press Change price to make the client send a PATCH request.
The animation is slowed down: in reality the whole trip takes a few dozen milliseconds.
| id | name | price |
|---|
Enter a price — a number above zero.
There are no products left in the catalogue.
Done! The server returned 200 OK and the whole product. Look at the table: only the price changed in the row, and the name stayed the same. That is how PATCH works: what was sent gets changed.
What happens to the product name after a PATCH request that has only the price in its body?
So what should I check here?
Check three things. First: the response code is 200 OK, and the body has the product with the new price. Second: in the database table the price changed, while the name and the id stayed the same. Third: a separate GET /api/v1/products request returns the product with the new price, so customers will see it too.
Quick reference
The PATCH method
PATCH /api/v1/products/3 means “change partially”. The request body has only the fields that need to change; the others stay as they were.
Code 200 OK
A success response. For PATCH the server usually returns the whole product in the body, already with the changes.
PATCH and PUT
PATCH changes part of the data, PUT replaces the object completely. To fix a single field, PATCH is more convenient.
What a tester checks
The response code is 200, and the body has the product with the new price. In the database only the price changed; the name and the id are the same. The new price is visible in the admin panel and in the catalogue.