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.
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.