7. PATCH and DELETE
Task: Checkout your book
Someone wants to checkout the book your just added! As a librarian, you will update the library database via the API to mark the book’s checkedOut
status from false
to true
. The API documentation shows we can update a book by id by making a request (authorized with the API Key) with the updated info to: PATCH https://library-api.postmanlabs.com/books/:id
.
Make a request to update the book
- Hover over your Postman Library API v2 collection, click the three dots, select Add request, and name it checkout a book.
- Set the request method to
PATCH
. - Set the request URL to: /books/:id.
Set the value of the path variable
id
to . This will use the value of our collection variable namedid
that was set in the Test script of the add a book request.Add a raw JSON body in the Body tab to update the
checkedOut
property totrue
:1 2 3
{ "checkedOut": true }
Save and Send the request. You should get a
200 OK
response that shows the updated data about your book.
Now, if you return to your get book by id request, update the id path variable to , Save, and Send the request, you will see the same updated data:
Task: Delete your book
Oops! The person that checked out your book accidentally lost it…you will need to delete it from the library database. The API documentation shows how we can delete books with the DELETE /books/:id path
.
Make a new request
The DELETE
request has a similar format to the PATCH
request, so let’s copy the PATCH
request to make our new request.
- Hover on your Collection, Add request, and name it delete a book.
- Set the request method to
DELETE
. - Set the request URL to /books/:id.
- In the Params tab, make sure tha path variable id is set to .
- Save and Send your request. You should get a
204 No Content
response from the API. This means the server successfully deleted the book, and won’t send any response body back.
Is it really gone?
Without changing anything, try sending your request again. Since you are sending a request to delete a book with an id that no longer exists, you get a 404
error: