Tutorial: Update an existing task

In this tutorial, you learn to update an existing task resource instance using the PUT method. In REST API, the PUT method replaces an existing instance with new information. In this case, you must know the exact ID of the task resource.

Expect this tutorial to take about 10 minutes to complete.

You learn

In this tutorial you learn to:

  • Update an existing task using Postman
  • Update an existing task using curl command

Prerequisites

  1. Review and complete Before you start a tutorial topic on your development system.
  2. Task resource id of the task you want to update.

Update an existing task using Postman

To update an existing task using Postman:

  1. Start the local server, ignore if server is already started.

     cd <your-github-workspace>/to-do-service-sp26/api
     json-server -w to-do-db-source.json
    
  2. Open Postman desktop app.
  3. In the Postman app, create a new request with following values:
    • METHOD: PUT
    • URL: {base_url}/tasks/{task-id}
    • Request body: Change the values of each property as you’d like.

        {
            "userId": 11,
            "title": "Buy Flowers",
            "description": "Get beautiful Sunflowers and Lillies",
            "dueDate": "2026-05-10T17:00",
            "warning": "8"
        }
      
  4. Click Send to make the request.
  5. Check the task changes in response body.

     {
         "userId": 11,
         "title": "Buy Flowers",
         "description": "Get beautiful Sunflowers and Lillies",
         "dueDate": "2026-05-10T17:00",
         "warning": "8",
         "id": 1
     }
    

Note: Verify that the information matches to what you provided.

Validation

To test the changes:

  1. In the Postman app, create a new request with following values:
    • METHOD: GET
    • URL: {base_url}/tasks/{task-id}

    Mention the task ID that you used in the procedure. You should get the updated user details in the response body.

Update an existing task using curl

To update an existing task using curl:

  1. Start the local server, ignore if server is already started.

     cd <your-github-workspace>/to-do-service-sp26/api
     json-server -w to-do-db-source.json
    
  2. Open a new terminal window.
  3. Navigate to your GitHub project directory.
  4. Type following in your terminal. Change the values of each property as you’d like.

         curl -X PUT http://{base_url}/tasks/{task-id} \
         -H "Content-Type: application/json" \
         -d '{
                 "userId": 11,
                 "title": "Walk Dog",
                 "description": "NA",
                 "dueDate": "2026-05-10T17:00",
                 "warning": "9",
                 "id": {task-id}
             }
    
  5. Press enter.
  6. Terminal should return the exact information on successful update.

Testing

To test the changes:

  1. In the terminal, type following:

         curl http://{base_url}/tasks/{task-id}
    

    Mention the task ID that you used in the procedure. You should get the updated user details in the response body.

Example


        $ curl -X PUT http://localhost:3000/tasks/4 \
        -H "Content-Type: application/json" \
        -d '{
                "userId": 4,
                "title": "Walk Dog",
                "description": "NA",
                "dueDate": "2026-05-10T17:00",
                "warning": "9",
                "id": 4
            }'

        {
            "userId": 4,
            "title": "Walk Dog",
            "description": "NA",
            "dueDate": "2026-05-10T17:00",
            "warning": "9",
            "id": 4
        }

        $ curl http://localhost:3000/tasks/4
        {
                "userId": 4,
                "title": "Walk Dog",
                "description": "NA",
                "dueDate": "2026-05-10T17:00",
                "warning": "9",
                "id": 4
        }    

References

  • Add a new task
  • Delete a task