Skip to main content

Welcome to a Grasp lesson

Create a free account to keep studying and create your own study path.

Lesson 43

Building RESTful Endpoints with CRUD Operations

Recommended resources

Introduction

In the previous lesson, you did a rapid run-through of creating a full Postman collection, setting up an environment, and adding all the necessary requests for your API. That was excellent, high-speed work.

Now, we'll take a moment to deliberately practice and solidify the patterns for creating each of the core CRUD (Create, Read, Update, Delete) requests. The goal here is to make these actions second nature. Under exam pressure, you need to be able to configure GET, POST, PUT, and DELETE requests quickly and without hesitation. This lesson is a focused drill to build that muscle memory.

We will systematically review how to configure each request type, paying close attention to when to use path variables (for targeting specific resources) and request bodies (for sending data).


Reviewing the CRUD Operations in Postman

You already have your collection and Local Development environment set up. Ensure your Node.js server is running so you can test the requests as we build them. We will review the pattern for each of the four main operations.

This short video from the Postman team provides a perfect visual summary of what we're about to practice.

REST API Fundamentals: Learn to Use GET, POST, PUT, & DELETE

Watch this quick video, 'REST API Fundamentals', to see a demonstration of how to construct GET, POST, PUT, and DELETE requests in Postman.

Watch from 01:15 to 04:18. Pay attention to how each HTTP method (GET, POST, PUT, DELETE) corresponds to a different action and how the request is configured in Postman (e.g., using parameters, setting the request body).

Now, let's break that down step-by-step for your API.

1. GET Requests (Read)

You have two primary GET requests: fetching all resources and fetching a single resource by its ID.

  • GET All: This is the simplest request.

    • Method: GET
    • URL: {{baseUrl}}/api/products
    • No path variables or body are needed.
  • GET by ID: This request uses a path variable to specify which resource to fetch.

    • Method: GET
    • URL: {{baseUrl}}/api/products/:id
    • When you type :id in the URL, Postman automatically creates a "Path Variables" section below the URL bar where you can paste the ID of an existing product.
Postman Interface: Defining Path Variables
This image shows how Postman automatically detects a path variable (like `:record` or `:id`) from the URL and provides a field for you to enter its value.

2. POST Request (Create)

A POST request creates a new resource. It requires a request body containing the data for the new item.

  • Method: POST
  • URL: {{baseUrl}}/api/products
  • Body:
    1. Select the Body tab.
    2. Choose the raw option.
    3. Select JSON from the dropdown menu.
    4. Provide the data for your new product in valid JSON format.
Postman POST Request with JSON Body and Environment Variable
This image shows the 'Body' tab in Postman configured to send a raw JSON payload. This is the standard way to send data for POST and PUT requests.

3. PUT Request (Update)

A PUT request updates an existing resource. It combines the patterns from GET by ID and POST: it uses a path variable to identify the resource and a request body to provide the updated data.

  • Method: PUT
  • URL: {{baseUrl}}/api/products/:id
  • Body: Configure the body with the updated fields, just like you did for the POST request.

4. DELETE Request (Delete)

A DELETE request removes a resource. It only needs to know which resource to remove, so it uses a path variable and does not require a body.

  • Method: DELETE
  • URL: {{baseUrl}}/api/products/:id
  • No body is needed.

Practical Exercise: Verify Your Collection

Your task is to review the requests in your Postman collection to ensure they are all configured correctly. This will solidify your understanding and prepare you for the exam.

Action Steps:

  1. POST: Send your Create New Product request. Copy the _id from the JSON response.
  2. GET by ID: Go to your Get Product by ID request. Paste the copied _id into the id path variable's value field. Send the request and verify you receive the correct product with a 200 OK status.
  3. PUT: Go to your Update Product request. Use the same _id. Change a value in the JSON body (e.g., update the price). Send the request and verify you get the updated product back with a 200 OK status.
  4. DELETE: Go to your Delete Product request. Use the same _id. Send the request. You should receive a 204 No Content status.
  5. Confirm Deletion: Go back to the Get Product by ID request and send it again with the same ID. Verify that you now receive a 404 Not Found error.

For a consolidated written reference of these steps, you can review the following guide.

API Testing with Postman — A Step-by-Step Guide

This article, 'API Testing with Postman — A Step-by-Step Guide', provides a clear, written summary of how to set up requests for all basic CRUD operations. It's a great reference to have.

Quickly read the section titled '2. Basic CRUD Operations: Testing the API Endpoints'. It covers GET, POST, PUT, and DELETE requests, reinforcing the patterns you just practiced.


Conclusion

Mastering these four request patterns in Postman is fundamental. By repeatedly and deliberately practicing their setup, you ensure that you can test and demonstrate your API's functionality under pressure.

Key Takeaways:

  • GET by ID, PUT, and DELETE all use a path variable (e.g., /:id) to target a specific resource.
  • POST and PUT both require a request body, typically in raw JSON format, to send data to the server.
  • The workflow of Create -> Read -> Update -> Delete is a common and effective way to test your entire API.

Next Up:

You've now thoroughly covered creating a Postman collection and adding all the necessary requests. The next lesson will have you formalize the use of the {{baseUrl}} environment variable, a key professional practice we've already started using. After that, we'll cover the final step in the Postman workflow: exporting your collection for submission.