To create a new Todo using POST, we need to include the JSON for the Todo in the body of the request.
Let's consider an example—we want to create a new Todo for Jack to Learn Spring Boot.
Some important details are listed here:
- We would need to send a POST request to the following URI: http://localhost:8080/users/Jack/todos.
- The body of the request contains JSON; therefore, we would need to send a request header—Content-Type:application/json.
The complete request details are as follows:
Header
Content-Type:application/json
Body
{
"user": "Jack",
"desc": "Learn Spring Boot",
"done": false
}
The following screenshot shows how we can use the Postman app to create the request, and the response after executing the request:
A few important things to note are as follows:
- We are sending a POST request; therefore, we choose POST from the top-left dropdown.
- To send the Todo JSON as part of the body of the request, we select the raw option in the Body tab (highlighted with a blue dot). We choose the content type as JSON (application/json).
- Once the request has been successfully executed, you can see the status of the request in the bar in the middle of the screen: Status: 201 Created.
- The location is http://localhost:8080/users/Jack/todos/5. This is the URI of the newly created todo that is received in the response.