Skip to content

How To Use Postman In Linux

API Testing Blog

Downloading and Installing Postman

Postman isn’t directly available in Linux repositories, so we’ll install it through their official website:

  1. Download Postman: Visit the Postman website (https://www.postman.com/) and click on “Download.” Choose the appropriate Linux version (AppImage, Debian, or RPM) based on your operating system.
  2. Install the AppImage: If you downloaded the AppImage:
    • Open a terminal and navigate to the downloaded directory.
    • Make the AppImage file executable:
      Terminal window
      chmod +x Postman-linux-*.AppImage
    • Run the AppImage:
      Terminal window
      ./Postman-linux-*.AppImage
  3. Install the Debian or RPM package:
    • Open a terminal and use your distribution’s package manager:
      • Debian/Ubuntu:
        Terminal window
        sudo dpkg -i Postman-linux-*.deb
      • Fedora/CentOS/RHEL:
        Terminal window
        sudo yum install Postman-linux-*.rpm
    • Follow the on-screen instructions to complete the installation.

Setting Up Postman for API Testing

Once installed, we need to configure Postman for effective API testing:

1. Creating a Workspace

  • Open Postman and click on “Workspaces” in the left sidebar.
  • Click on “Create Workspace” and choose a name for your workspace (e.g., “API Testing”).
  • You can create different workspaces for different projects or teams if needed.

2. Building API Requests

Postman uses a simple interface to craft your API requests:

  • Create a New Request:
    • Click on the ”+” icon in the top-left corner or choose “New” > “Request” from the menu.
  • Enter Request Details:
    • Method: Choose the HTTP method from the dropdown (GET, POST, PUT, DELETE, etc.).
    • URL: Enter the API endpoint URL.
    • Headers: Add any necessary headers (e.g., Content-Type, Authorization).
    • Body: Depending on the request method, you may need to provide data in the body:
      • Text: For plain text data.
      • JSON: For JSON data (commonly used for web APIs).
      • Form Data: For sending files or key-value pairs.
      • Binary: For uploading raw binary data.

Example:

  • Get request:
    GET https://reqres.in/api/users?page=2
  • Post request:
    POST https://reqres.in/api/users
    Content-Type: application/json
    {
    "name": "morpheus",
    "job": "leader"
    }

Sending API Requests and Analyzing Responses

1. Sending API Requests

  • Once you’ve configured the request, click the “Send” button. Postman will execute the request and display the response in the right pane.

2. Analyzing the Response

  • Status Code: The first thing to look at is the status code, which indicates the success or failure of your request.
    • 2xx: Success
    • 4xx: Client-side error
    • 5xx: Server-side error
  • Headers: Examine the response headers for additional information about the response.
  • Body: The content of the response. You can view it raw, as formatted JSON, as plain text, or as a preview.

Using Postman for API Testing in Linux

1. Validating Responses with Assertions

Postman allows you to define assertions to verify the expected behavior of your API. For example, you can verify that the response status code is 200 or that the response body contains specific data:

[
{
"test": "Status code is 200",
"script": "pm.test(\"Status code is 200\", function () {pm.response.to.have.status(200);});"
},
{
"test": "Body has 'morpheus' name",
"script": "pm.test(\"Body has 'morpheus' name\", function () {pm.expect(pm.response.json().name).to.equal(\"morpheus\");});"
}
]
  • Creating assertions:
    • Click on the “Tests” tab.
    • Paste the assertion code into the editor.
    • You can use the “Test” button to run the tests.

2. Automating API Testing

Postman Collections and Runner allow you to organize and automate your API tests:

  • Creating Collections:
    • Collections group related requests together, making them easier to manage.
    • You can add individual requests to a collection and organize them into folders.
  • Running Collections:
    • Postman Runner enables you to execute entire collections and run tests within them.
    • You can define environment variables (e.g., for API keys or different environments) and set up iterations for running tests multiple times with different parameters.

Example:

  • Collection structure:

    API Testing
    - Users
    - Get all users
    - Create a new user
    - Update a user
    - Delete a user
    - Posts
    - Get all posts
    - Create a new post
    - Update a post
    - Delete a post
  • Running a collection:

    • Click on the “Runner” tab.
    • Choose the collection you want to run.
    • Set environment variables (optional).
    • Set up iterations (optional).
    • Click “Run.”

Conclusion

Postman is a powerful tool for API testing in Linux. It provides a user-friendly interface for creating, sending, and analyzing API requests, as well as features for validating responses, automating tests, and collaborating with teams. By mastering these principles, you’ll be well-equipped to effectively test and ensure the quality of your web APIs.

API Testing Blog