Testing PetStore APIs Using Postman: A Comprehensive Guide

 


Testing PetStore APIs Using Postman: A Comprehensive Guide

APIs play a crucial role in modern web applications, enabling different systems to communicate seamlessly. The PetStore API is a popular example used for learning and testing API functionalities. In this blog, we'll cover how to test PetStore APIs using Postman, focusing on API correlation and CRUD (Create, Read, Update, Delete) operations.

 Prerequisites

- Postman installed on your machine. You can download it from [here](https://www.postman.com/downloads/).
- Basic understanding of RESTful APIs.
- An account on a PetStore API provider or a local instance of the PetStore API.

 Step 1: Setting Up Postman

1. Launch Postman and create a new workspace or use an existing one.
2. Create a new Collection by clicking on `New` > `Collection`. Name it "PetStore API Testing".

 Step 2: Testing CRUD Operations

1. Create a New Pet (POST Request)

To add a new pet to the store, we'll use the POST method.

- Endpoint: POST /v2/pet
- Request Body:
 
  {
    "id": 123456,
    "category": {
      "id": 1,
      "name": "Dogs"
    },
    "name": "Max",
    "photoUrls": ["http://example.com/photo.jpg"],
    "tags": [
      {
        "id": 1,
        "name": "tag1"
      }
    ],
    "status": "available"
  }

- Steps:
  1. In Postman, create a new request in the "PetStore API Testing" collection.
  2. Set the request type to POST.
  3. Enter the endpoint URL: https://petstore.swagger.io/v2/pet.
  4. Go to the Body tab, select raw and JSON, then paste the above JSON body.
  5. Click Send.

If successful, you'll get a response with the details of the newly created pet.

2. Read Pet Details (GET Request)

To fetch the details of a pet, we'll use the GET method.

- Endpoint: GET /v2/pet/{petId}
- Steps:
  1. Create a new request in the "PetStore API Testing" collection.
  2. Set the request type to GET.
  3. Enter the endpoint URL: https://petstore.swagger.io/v2/pet/123456.
  4. Click Send.

The response should contain the details of the pet with ID 123456.

3. Update Pet Details (PUT Request)

To update an existing pet's details, we'll use the PUT method.

- Endpoint: PUT /v2/pet
- Request Body:
 
  {
    "id": 123456,
    "category": {
      "id": 1,
      "name": "Dogs"
    },
    "name": "Maximus",
    "photoUrls": ["http://example.com/photo.jpg"],
    "tags": [
      {
        "id": 1,
        "name": "tag1"
      }
    ],
    "status": "available"
  }

- Steps:
  1. Create a new request in the "PetStore API Testing" collection.
  2. Set the request type to PUT.
  3. Enter the endpoint URL: `https://petstore.swagger.io/v2/pet`.
  4. Go to the Body tab, select raw and JSON, then paste the above JSON body.
  5. Click Send.

The response should confirm the updated details of the pet.

4. Delete a Pet (DELETE Request)

To remove a pet from the store, we'll use the DELETE method.

- Endpoint: DELETE /v2/pet/{petId}
- Steps:
  1. Create a new request in the "PetStore API Testing" collection.
  2. Set the request type to DELETE.
  3. Enter the endpoint URL: `https://petstore.swagger.io/v2/pet/123456`.
  4. Click Send.

The response should confirm the deletion of the pet with ID 123456.

 Step 3: API Correlation

API correlation involves using data from one API response in subsequent API requests. This is particularly useful for creating workflows where the output of one request is the input for another.

Example Workflow: Adding a Pet and Retrieving Its Details

1. Create a New Pet:
   - Use the POST /v2/pet endpoint to create a new pet.
   - Extract the pet ID from the response.

2. Retrieve Pet Details:
   - Use the extracted pet ID from the previous response to fetch the pet's details using the GET /v2/pet/{petId} endpoint.

Steps in Postman:

1. Create a New Pet Request:
   - Follow the steps in the "Create a New Pet" section.
   - In the Tests tab, add the following script to store the pet ID in an environment variable:
     var jsonData = pm.response.json();
     pm.environment.set("petId", jsonData.id);

2. Retrieve Pet Details Request:
   - Follow the steps in the "Read Pet Details" section.
   - Replace 123456 in the endpoint URL with the environment variable: https://petstore.swagger.io/v2/pet/{{petId}}.

With these steps, you can automate the workflow of adding a pet and retrieving its details using Postman.

 Conclusion

Testing APIs is a crucial aspect of modern software development. With Postman, you can efficiently test CRUD operations and implement API correlation to simulate real-world workflows. The PetStore API provides an excellent playground for honing your API testing skills.

Post a Comment

Previous Post Next Post