Insomnia: PUT
Before you write the PUT method, save the request that changes a category in Insomnia, with the id in the address and the whole changed category in the body, and decide what it should answer.
Journeyman · Spring Boot · REST API · Updated
After this lesson you can
- Send a PUT request to a Spring Boot API from Insomnia, with the item's id in the address and the whole item in the body
Key takeaways
- A PUT changes one item. The address says which item, with its id at the end, and the body holds the whole item as it should be.
- Use the id your own API gave Snacks. The body of Add Snacks' answer says it.
- The answer you want is 200 OK with the item as it's now saved.
- A PUT replaces the whole item, so a field left out of the body is emptied.
Your API can read and add categories. Next it will change them. Snacks was saved as "Chips, nuts and pretzels", and the shop now sells popcorn too. As before, write down what you want the API to answer first.
Which category?
A PUT goes to one item's address, with the item's id at the end, and carries a body, like POST. The address says which category; the body says what it should look like now.
You need Snacks' id. Open your Add Snacks request in Insomnia and look at its last answer: the body gives the categoryId. It's 10 if you sent Add Snacks once; if you sent it more times, there are several Snacks, and you can use any of their ids. This lesson uses 10.
A request that changes Snacks
Add a request to the Northwind API collection, name it Update Snacks, and set it to:
PUT {{ _.base_url }}/api/categories/10
On the Body tab, choose JSON, and type the whole category as it should be:
{
"categoryName": "Snacks",
"description": "Chips, nuts, pretzels and popcorn"
}
Run your app, and send it. The answer is 404 Not Found: so far your controller answers only /api/categories, so nothing answers at /api/categories/10 at all. That's a 404, not a 405: the address itself is new.
The answer you want
Once the next lesson is built, Update Snacks should answer 200 OK, with the category as it's now saved:
{
"categoryId": 10,
"categoryName": "Snacks",
"description": "Chips, nuts, pretzels and popcorn"
}
Then check it the way any client would: send All categories, and find category 10 at the end of the list, with its new description.
Send the whole item
A PUT replaces the item. Whatever the body leaves out is emptied, not kept. Duplicate Update Snacks (right-click it, Duplicate), name the copy Update Snacks, name only, and take the description out of its body:
{
"categoryName": "Snacks"
}
Once the method is written, this one answers 200 OK with "description": null. That isn't a bug in your API: the update sets every column from the body, and the body had no description. A client that wants to keep the description sends it again.
Do it: work out what a PUT saves
Before you send Update Snacks, name only, work out what it should answer. A PUT replaces the item: the id comes from the address, and everything else comes from the body, even what the body leaves out. Write a method that works out the category a PUT saves.
Category is given at the bottom of the file, with getCategoryId(), getCategoryName() and getDescription(). A description the body leaves out arrives as null.
The method gets two values:
id: the id at the end of the address, anint, such as10fromPUT /api/categories/10.body: the category from the request's body, aCategory. It has no id (its id is0), and its description may benull.
For example, Update Snacks, name only, PUT /api/categories/10 with the body { "categoryName": "Snacks" }:
| categoryId | categoryName | description | |
|---|---|---|---|
| The body | 0 |
Snacks |
null: left out |
| What the PUT saves | 10, from the address |
Snacks |
null |
So saved(10, new Category(0, "Snacks", null)) returns a category with the id 10, the name Snacks and the description null.
Your job: write the method saved(int id, Category body) so it returns a new Category: what the PUT saves.
- Its id is
id, from the address, never the body's. - Its name and its description are the body's, even when the body's description is
null.
Practiced live on the lesson page: write the Java right there and check it.
Check yourself
2 questions to try on the lesson page.
So what
Two to-do items this time: change Snacks, and see what replace means. In the next lesson you write the PUT method and send them both again.