Post

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

  1. Hover over your Postman Library API v2 collection, click the three dots, select Add request, and name it checkout a book.
  2. Set the request method to PATCH.
  3. Set the request URL to: /books/:id.
  4. Set the value of the path variable id to . This will use the value of our collection variable named id that was set in the Test script of the add a book request.

  5. Add a raw JSON body in the Body tab to update the checkedOut property to true:

    1
    2
    3
    
     { 
     "checkedOut": true 
     }
    
  6. 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.

  1. Hover on your Collection, Add request, and name it delete a book.
  2. Set the request method to DELETE.
  3. Set the request URL to /books/:id.
  4. In the Params tab, make sure tha path variable id is set to .
  5. 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:

This post is licensed under CC BY 4.0 by the author.