Skip to content

How To Test Swagger Api Using Postman

API Testing Blog

How to Test a Swagger API Using Postman

Swagger is a popular API documentation framework that helps you design, build, and document your APIs. Postman is a powerful tool for testing APIs that can be used to test your Swagger-defined APIs effectively. This guide will walk you through the steps of testing a Swagger API using Postman, complete with practical examples and code snippets.

Understanding Swagger and Postman

Swagger makes it easy to define and document your APIs using a simple YAML or JSON format. This definition file, often called OpenAPI Specification, clearly describes the API endpoints, request parameters, data models, and responses. This information is then used to generate interactive documentation, client libraries, and server stubs.

Postman is a platform for building, testing, documenting, and sharing APIs. It allows you to send requests to your APIs, view the responses, and analyze the results. Postman also has powerful features like environment variables, tests, and collections that streamline API testing.

Step-by-Step Guide: Testing a Swagger API with Postman

1. Import the Swagger Definition

  1. Obtain the Swagger definition file: Download or copy the YAML or JSON file containing the Swagger specification.
  2. Open Postman: Launch Postman and select “Import” from the menu.
  3. Select “Swagger” as the import type and choose the Swagger definition file you saved.
  4. Import the API: Postman will process the Swagger file and import the definition into a new collection.

2. Explore the API Collection

  1. Navigate the collection: The newly imported collection will contain all the endpoints defined in your Swagger specification. You can explore the collection and view the details of each endpoint.
  2. Inspect request parameters: Each endpoint will show the request method (GET, POST, PUT, DELETE, etc.), URL path, request headers, and request body parameters.
  3. Understand responses: Postman will also display the expected response structure (including status code, headers, and body) for each endpoint.

3. Send API Requests

  1. Choose an endpoint: Select the endpoint you want to test from the collection.
  2. Modify request parameters (optional): You can modify default values for request parameters, headers, or body data.
  3. Send the request: Click the “Send” button to execute the request to your API.

4. Analyze the Response

  1. Review the Status Code: Check the HTTP status code in the response. A 200 status code usually indicates success, while other codes suggest issues or errors.
  2. Inspect Response Headers: Examine the response headers to check for any important information or error messages.
  3. Analyze Response Body: Check the body of the response to verify the data returned by the API. You can use Postman’s built-in tools to:
    • Format: Format the response data in different formats (JSON, XML, HTML, etc.).
    • Preview: View the response data in a user-friendly format.
    • Assertions: Use Postman’s built-in assertions to verify the response data and ensure its correctness.
    • Code Generation: Use Postman’s code generation features to automatically generate client code for your API in different languages.

Example: Testing a Weather API with Postman

Let’s use a simple weather API to demonstrate testing a Swagger API with Postman. Assume we have a Swagger specification defining an endpoint for getting current weather data:

openapi: 3.0.0
info:
title: Weather API
version: v1
paths:
/weather:
get:
summary: Get current weather information
parameters:
- in: query
name: city
schema:
type: string
required: true
responses:
'200':
description: Successful response
content:
application/json:
schema:
type: object
properties:
city:
type: string
temperature:
type: number
description:
type: string

1. Import the Swagger Definition

Import the above YAML file into Postman using the steps outlined in Section 1.

2. Explore the API Collection

Postman will create a collection with the ‘weather’ endpoint. You can see the ‘GET’ method, the ‘city’ query parameter, and the expected response structure defined in the Swagger spec.

3. Send API Request

  1. Select the ‘weather’ endpoint in the collection.
  2. In the params tab, set the ‘city’ query parameter to ‘New York’.
  3. Click ‘Send’.

4. Analyze the Response

  1. Status Code: The response should display a 200 status code, signifying success.
  2. Response Headers: You can see the response headers for content type, etc.
  3. Response Body: The response body will contain JSON data with the city name, temperature, and weather description for New York City.

5. Assertions

Postman can be used to write assertions to check that the response data is correct. You can create a test script to check if the response body has the expected data. Here’s an example test script:

pm.test("Status code is 200", function () {
pm.response.to.have.status(200);
});
pm.test("Response body has a city name", function () {
pm.response.to.have.body('city');
});
pm.test("Response body has a temperature", function () {
pm.response.to.have.body('temperature');
});
pm.test("Response body has a weather description", function () {
pm.response.to.have.body('description');
});

Run the test to verify that your API is functioning as expected.

These are just some examples of the variety of tests that can be performed on a Swagger API via Postman. By following these steps and incorporating custom tests, you can ensure that your API is robust, reliable, and meets the desired functionality.

API Testing Blog