Insomnia: PUT
Before you build the PUT action, write the requests that change a category in Insomnia, with the id in the address and the whole changed category in the body, and decide what each one should answer.
Journeyman · REST API · ASP.NET Core · Updated
After this lesson you can
- Send a PUT request 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 Location header on Add Snacks' answer says it.
- The answers you want are 200 OK with the item as it's now saved, and 404 Not Found for an id that doesn't exist.
- 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, like GET for one category, 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 Location header and the body both give it. It's 9 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 9.
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/9
On the Body tab, choose JSON, and type the whole category as it should be:
{
"categoryName": "Snacks",
"description": "Chips, nuts, pretzels and popcorn"
}
Start your API, and send it. The answer is 405 Method Not Allowed: the address /api/categories/9 exists (your GET answers there), but nothing answers PUT there yet. The Headers tab shows Allow: GET.
The answers you want
Once the next lesson is built, Update Snacks should answer 200 OK, with the category as it's now saved:
{
"categoryId": 9,
"categoryName": "Snacks",
"description": "Chips, nuts, pretzels and popcorn"
}
Then check it the way any client would: ask for category 9 with a GET. Duplicate Category 8 (right-click it, Duplicate), name the copy Category 9, and change its address to /api/categories/9. It should show the new description too.
Add one more request, Update a missing category: the same body, sent to an id no category has.
PUT {{ _.base_url }}/api/categories/300
The answer you want is 404 Not Found: there's nothing at 300 to change.
Send the whole item
A PUT replaces the item. Whatever the body leaves out is emptied, not kept. Duplicate Update Snacks, name it Update Snacks, name only, and take the description out of its body:
{
"categoryName": "Snacks"
}
Once the action is built, this one answers 200 OK with "description": null. That isn't a bug in your API: it's what a PUT means. A client that wants to keep the description sends it again.
Do it: read the new id from the Location header
To write Update Snacks, you need the id your API gave Snacks. The Location header on Add Snacks' answer holds it, at the end of the address. Write a method that reads it.
The method gets one value:
location: theLocationheader's value, astring. It always ends with a/and then the id.
For example, "http://localhost:5179/api/categories/9":
| Step | Here |
|---|---|
The part after the last / |
"9", text |
| As a number | 9 |
So IdFromLocation("http://localhost:5179/api/categories/9") returns 9.
Your job: write the method IdFromLocation(string location) so it returns the id, an int.
- Take the part of
locationafter the last/. - Turn it into an
int, and return it.
Practiced live on the lesson page: write the C# right there and check it.
Check yourself
2 questions to try on the lesson page.
So what
Four to-do items this time: change Snacks, check it with a GET, refuse an id that doesn't exist, and see what replace means. In the next lesson you build the PUT action and send them all again.