The POST Method: Adding a Product
How an administrator adds a product to the catalogue: a POST request, the request body, code 201, and a new row in the database.
About the product
Hi! You already know how a customer loads the BB Food catalogue. Now let’s look at the other side: the shop has an admin panel where an administrator adds new products. Here the client no longer just reads data — it sends data to the server.
Interesting! So how does it work?
Kadri will explain it best — Kadri is already waiting for you in the meeting room. Today we only cover the positive scenario: everything goes as it should.
Hi! Remember the GET method? It gets data and changes nothing. But an administrator needs something else: to create a new product in the catalogue. HTTP has the POST method for that.
Hi Kadri! So how is POST different from GET?
GET means “get”, and POST means “create”. The client sends a POST request together with the data of the new product. Here is what it looks like:
Request
POST /api/v1/products
{
"name": "Cottage cheese, 200 g",
"price": 1.89
}- the method — what to do.
POSTmeans “create” - the address — what to do it with.
/api/v1/productsis the same product list as in GET - the request body — the data the client sends to the server. Here it is the name and the price of the new product, in JSON. There is deliberately no
idin the request: the client doesn’t send it, the database on the server side assigns the number to the new product
And what does the server answer to such a request?
The server saves the product in the database and sends back a response. The code 201 Created means “created” — it is the success response specifically for POST. In the response body the server returns the created product. Note the id: the client didn’t send it, the database assigns the number.
Response
201 Created
{
"id": 8,
"name": "Cottage cheese, 200 g",
"price": 1.89
}- the status code — how it went.
201 Createdmeans “created” - the response body — the created product, now with an
id
Note. By the HTTP standard, a creation is answered with 201 Created. But in practice you may also see 200 OK: it depends on how the server is built. If the team agreed that a creation returns 200, it is not a bug. Just remember that this happens, and check the API documentation.
Let’s check you’ve got it. An administrator filled in the form and pressed “Add product”. Which method will the client send?
Let’s see it in practice!
Here is the BB Food admin panel. An example product is already in the form, and you can change it. Press Add product and follow the path of the request: client → server → database → server → client.
Application under test
Scenario. Positive: the administrator adds a product
Enter a name and a price — a number above zero.
The server is waiting for requests. Press Add product to make the client send POST /api/v1/products.
The animation is slowed down: in reality the whole trip takes a few dozen milliseconds.
| id | name | price |
|---|---|---|
| 1 | Bananas, 1 kg | 1.49 |
| 2 | Cherry tomatoes, 250 g | 2.29 |
| 3 | Milk 2.5%, 1 l | 1.19 |
| 4 | Gouda cheese, 200 g | 2.99 |
| 5 | Rye bread | 1.79 |
| 6 | Orange juice, 1 l | 2.49 |
| 7 | Salted herring, 300 g | 3.29 |
Done! The product is saved in the database, and the server returned 201 Created and the created product together with its id. Look at the table: a new row has appeared. So POST, unlike GET, changed the data on the server.
And what happens if the administrator presses “Add product” once more with the same data?
So what should I check here?
Check three things. First: the response code is 201 Created (or whatever the team agreed on). Second: the response has an id, and the name and price are what the administrator entered. Third: the product appeared in the database table, which means a customer will see it in the catalogue when the client requests the product list with GET /api/v1/products. POST itself doesn’t show that list — it takes a separate GET request. That is the positive scenario of POST.
Quick reference
The POST method
POST /api/v1/products means “create”. The client sends data in the request body, and the server saves it in the database. Unlike GET, POST changes data on the server.
Request body
The data the client sends to the server, most often in JSON. For a new product it is the name and the price.
Code 201 Created
The success response to a creation. In the response body the server usually returns the created object, already with its identifier.
Identifier (id)
The client doesn’t send it: the unique number of a new record is assigned by the database.
Repeating POST
Every POST creates a new record, so a repeated request with the same data produces a duplicate. GET has no such problem: it only reads data.
What a tester checks
The response code is 201 (or whatever the team agreed on). The response has an id, and the name and price match what was entered. A new row appeared in the database, and a customer sees the product in the catalogue.