You've now learned how POST requests work, allowing you to send new information to any API on the web. But what happens if you make a mistake? Or what if you want to update a task to mark it as completed? Do you have to make a DELETE request and then another POST request to change the resource?
The answer to that question is no. There is a way to fix the problem with a single HTTP request. Enter the HTTP PUT method. The PUT method instructs the server to update the resource at the specified location.
Making a PUT Request
Step 1: Submit a GET request to: http://demo.codingnomads.co:8080/tasks_api/tasks
Step 2: Make note of the Task ID you want to update.
Step 3: Open up a new Postman tab.
Step 4: Change the HTTP method to PUT.
Step 5: Plug in this URL: http://demo.codingnomads.co:8080/tasks_api/tasks/{ID}
Make sure to replace {ID} with the ID noted in Step 2.
Step 6: Copy the JSON below into the request body. Make sure your userId corresponds to a valid User.
{
"userId": 5,
"name": "name will be updated to this",
"description": "description will be updated to this",
"completed": true
}
Info: Notice how this request URL has an {ID} path variable while the POST request in the previous lesson did not. This is because you now must specify the ID of the resource you want to update in the URL. If you ignore this, the server won't know which Task you want to update. Depending on how the developer sets up the API, the server may need even more information before it can update the resource.
Step 7: Hit enter and check if the response has the updated Task.
Step 8: Confirm the resource has been updated by making a GET request. Remember that sometimes you may have attempted to change a field the server considers final. In most cases, this will not result in an error, but the field will not be updated.
Learn by Doing
Location: http://demo.codingnomads.co:8080/tasks_api/tasks
- Use PUT to update at least two existing tasks.
- Specify a different
userId,name, anddescriptioneach time.
Summary: Postman PUT Request
The PUT HTTP Method indicates to a server that a resource should be updated. To do this, the server needs an immutable field, like an ID, to identify the resource. Fields other than the unique identifier can typically be changed or left unchanged, depending on the API design and the permissions of the user making the request.