Skip to content

How To Test Web Api Using Postman

API Testing Blog

Getting Started with Postman for Web API Testing

Postman is a powerful tool for testing web APIs, offering a user-friendly interface and a wide range of features. This guide provides a comprehensive introduction to using Postman for API testing, covering key concepts and practical examples.

Understanding the Basics

Before diving into Postman, let’s understand the essentials of API testing:

  • API Endpoint: The specific URL that defines the resource you’re interacting with.
  • HTTP Methods: Actions you perform on the API, such as GET (retrieve data), POST (create data), PUT (update data), DELETE (remove data).
  • Request Headers: Additional information sent with the request, such as authentication tokens or content type.
  • Request Body: The data you send with the request, often in JSON format.
  • Response Status Code: A numerical code indicating the success or failure of the request.
  • Response Body: The data returned by the API, usually in JSON format.

Setting Up Postman

  1. Download and Install: Head over to https://www.postman.com/ and download the free version for your operating system.
  2. Create a Workspace: Workspaces organize your API requests, collections, and environments.
  3. Create a Collection: Collections group related API requests for better organization.
  4. Create a Request: Choose the appropriate HTTP method (GET, POST, PUT, DELETE) and enter the API endpoint URL.

Sending Your First Request

Let’s test a simple GET request to retrieve information from a fictional weather API:

API Endpoint: https://api.example.com/weather

  1. Add a Request: In your Postman workspace, click “New” and select “Request” to create a new request.
  2. Set Method and URL: Choose “GET” as the HTTP method and paste the API endpoint URL into the request input field.
  3. Send Request: Click the “Send” button to execute the request.

Examining the Response

After sending the request, Postman displays the response in different tabs:

  • Body: Contains the response data in its raw format (usually JSON).
  • Headers: Shows the response headers.
  • Cookies: Lists any cookies returned by the API.
  • Tests: Allows you to write automated tests for the response.
  • Code: Displays the HTTP status code (e.g., 200 for success).

Example Response:

{
"city": "New York",
"temperature": 72,
"condition": "Sunny"
}

Adding Request Headers

For more complex requests, you might need to add headers. For example, an API might require an API key for authentication:

API Endpoint: https://api.example.com/secure/data

  1. Add Headers Tab: Click the “Headers” tab in the request.
  2. Define Header: Click “Add” and enter the header key (e.g., “Authorization”) and value (e.g., “Bearer your_api_key”).

Sending POST, PUT, and DELETE Requests

For actions like creating, updating, or deleting data, you’ll use POST, PUT, and DELETE methods:

POST Example: Create a new user

{
"username": "john.doe",
"email": "john.doe@example.com"
}
  1. Choose Method: Select “POST” as the HTTP method.
  2. Add Body: Click the “Body” tab and choose “raw” as the format.
  3. Paste JSON: Paste the JSON data above into the body.
  4. Send Request: Execute the request to create the new user.

PUT Example: Update user information

{
"username": "john.doe",
"email": "john.doe@updated.com"
}
  1. Choose Method: Select “PUT” as the HTTP method.
  2. Add Body: Click the “Body” tab and choose “raw” as the format.
  3. Paste JSON: Paste the JSON data above into the body.
  4. Send Request: Execute the request to update user information.

DELETE Example: Delete a user

  1. Choose Method: Select “DELETE” as the HTTP method.
  2. Send Request: Execute the request to delete the user.

Writing Tests in Postman

Postman’s built-in testing capabilities allow you to automate API checks. This ensures your API behaves as expected throughout development and deployment.

Example test: Verify HTTP status code

pm.test("Status code is 200", function() {
pm.response.to.have.status(200);
});

Example test: Verify response body contains specific data

pm.test("Response contains 'city'", function() {
pm.expect(pm.response.text()).to.include('city');
});

Using Environments and Variables

Environments provide a way to manage different configurations for your API requests. This is useful for separating test environments (e.g., development, staging, production).

  1. Create Environment: Go to the “Environments” section in Postman.
  2. Define Variables: Add key-value pairs representing your environment-specific settings (e.g., API endpoints, API keys).
  3. Use Variables in Requests: Use double curly braces {{variable_name}} in your requests to reference environment variables.

Running Collections

Postman collections streamline running multiple API requests in a defined sequence.

  1. Add Requests to Collection: Add your requests to the collection you created earlier.
  2. Run Collection: Click on the collection and choose “Run” to execute all the requests in the defined order.

Advanced Features

Postman offers even more advanced features for API testing:

  • Data-Driven Testing: Use data files (CSV, JSON) to execute tests with multiple sets of data.
  • Mock Servers: Simulate API responses for testing scenarios.
  • Pre-request Scripts: Execute custom code before a request is sent.
  • Post-request Scripts: Execute custom code after a request has been completed.
  • Integrations: Connect Postman with various tools for continuous integration and collaboration.

Conclusion

Postman is a powerful and versatile tool for API testing, offering a comprehensive suite of features to streamline your workflow. From basic requests to complex automated tests, Postman empowers you to create robust and reliable APIs. By mastering the concepts outlined in this guide, you’ll be well-equipped to unleash the power of Postman for all your API testing needs.

API Testing Blog