Insomnia: POST

Before you write the POST method, save the request that adds a category in Insomnia, with a JSON body, send it, and learn why the answer today is 405 Method Not Allowed instead of 404.

Journeyman · Spring Boot · REST API · Updated

After this lesson you can

  • Send a POST request with a JSON body to a Spring Boot API from Insomnia
  • Tell 405 Method Not Allowed from 404 Not Found in a Spring Boot API

Key takeaways

  • A POST sends data in a body. In Insomnia, choose JSON on the Body tab and type the new item; Insomnia adds the Content-Type header for you.
  • 405 Method Not Allowed means the address exists, but nothing there answers that method. 404 Not Found means nothing answers at the address at all.
  • The answer you want from a POST is 201 Created, with the saved item and its new id in the body.
  • Every successful send of a POST adds another item, so expect a new id each time.

Your API can read categories. Next it will add them. As before, start by writing down what you want it to answer, as a request in your Northwind API collection.

A GET asks for something. A POST brings something: the new category, in the request's body. Think of it as the difference between asking a shop what's on the shelf and handing over a box to put on it.

A request with a body

In Insomnia, add a request to the Northwind API collection, name it Add Snacks, and set it to:

POST {{ _.base_url }}/api/categories

Then give it a body:

  1. Open the Body tab, under the address bar, and choose JSON.
  2. Type the new category:
{
  "categoryName": "Snacks",
  "description": "Chips, nuts and pretzels"
}

Choosing JSON also adds a header to the request, Content-Type: application/json, which tells the API what kind of body is coming. Open the Headers tab to see it. You don't type it yourself.

There's no categoryId in the body. The database chooses the new id, so the client doesn't send one.

405, not 404

Run your app, and send Add Snacks. The answer is 405 Method Not Allowed.

Why not 404 Not Found, as All categories answered before you wrote its method? Because the address isn't the problem. /api/categories exists now: All categories gets an answer from it. What's missing is a method that answers POST there.

Status Means
404 Not Found Nothing answers at this address, whatever the method
405 Method Not Allowed Something answers at this address, but not to this method

The answer's Headers tab shows which methods the address does accept: Allow: GET.

The answer you want

Once the next lesson is built, Add Snacks should answer 201 Created, which means "saved, and here it is", with the saved category in the body, now with the id the database gave it:

{
  "categoryId": 10,
  "categoryName": "Snacks",
  "description": "Chips, nuts and pretzels"
}

Each successful send adds another Snacks, with a new id: 10, then 11, then 12. That's what a POST does. If you send it a few times while you test, the extras are harmless in your own database. Send All categories afterwards, and the new categories are at the end of the list.

Check yourself

2 questions to try on the lesson page.

So what

A new to-do item in your collection, with a body this time. In the next lesson you write the POST method, and send Add Snacks again until it answers 201 Created.

Key terms

Cheat sheets