How To Test Api Calls Using Postman
Getting Started with Postman for API Testing
Postman is a powerful tool for testing APIs. It allows you to send requests to your API, inspect the responses, and automate tests. This guide will walk you through the basics of API testing using Postman, covering common scenarios and practical examples.
1. Setting up Postman for API Testing
1. Download and Install: Visit the Postman website (https://www.postman.com/) and download the app for your operating system.
2. Create a Workspace: A workspace acts as a container for your API requests, collections, and environments. You can create a new workspace by clicking on the “Workspaces” button in the top left corner.
3. Create a Request: Click the “New” button in the top right corner and select “Request” to create a new request.
2. Sending Simple API Requests
1. Select the HTTP Method: Choose the appropriate HTTP method from the dropdown menu (GET, POST, PUT, DELETE, etc.).
2. Enter the URL: Fill in the API endpoint URL in the address bar.
3. Add Parameters (if any):
- Query Parameters: Add query parameters in the “Params” tab.
- Headers: Add headers in the “Headers” tab.
- Body: For sending data, select the appropriate body format (form-data, raw, JSON, etc.) in the “Body” tab.
Example: Getting User Data:
- Method: GET
- URL:
https://api.example.com/users/1
Code (Postman Request):
GET https://api.example.com/users/1
3. Testing API Responses
After sending a request, Postman displays the response in the “Response” tab. You can inspect the following:
- Status Code: The HTTP status code indicates success or error (200: OK, 404: Not Found, etc.).
- Headers: The response headers contain information about the response.
- Body: The response body contains the actual data returned by the API.
Example: Verifying User Data:
- Expected Status Code: 200
- Expected Response Body (JSON):
{ "id": 1, "name": "John Doe", "email": "john.doe@example.com"}
Code (Postman Test):
pm.test("Status code is 200", function () { pm.response.to.have.status(200);});
pm.test("Response body contains expected user data", function () { pm.expect(pm.response.json().id).to.eql(1); pm.expect(pm.response.json().name).to.eql("John Doe"); pm.expect(pm.response.json().email).to.eql("john.doe@example.com");});
Explanation:
pm.test
allows defining test cases.pm.response.to.have.status
verifies the status code.pm.expect
is used for specific assertions.pm.response.json()
accesses the response body as JSON.
4. Building Collections for Organization
Organize your API tests into Collections, which are groups of requests. Collections help you structure your tests and make them easier to manage.
1. Create a Collection: Click the “Collections” button and create a new one. 2. Add Requests: Drag and drop or manually add your requests to the collection.
5. Automating API Tests with Environments
Environments in Postman allow you to manage different sets of variables like API URLs, credentials, and other configuration settings. This is useful for:
- Testing against different environments: Use different environments for development, staging, and production.
- Keeping sensitive information secure: Store credentials in environment variables.
1. Create an Environment: In the “Environments” section, click the “Add” button.
2. Define Variables: Define your variables and their corresponding values.
3. Use Variables in Requests: Use the {{variableName}}
syntax to reference environment variables in your requests.
Example: Using Different URLs in Environments:
Variable Name | Development Value | Production Value |
---|---|---|
apiUrl | http://dev.api.com/ | http://api.com/ |
Code (Postman Request):
GET {{apiUrl}}/users/1
6. Using Postman Runner for Automated Tests
Postman Runner allows you to execute your collections and tests as a batch. This facilitates automated API testing and reporting.
1. Navigate to Runner: Click on the “Runner” button to access the Postman Runner. 2. Select Collection and Environment: Choose the collection and environment you want to use. 3. Run Tests: Click on the “Run” button to start the tests.
Postman Runner provides a detailed report showing the results of each test, including:
- Passed/Failed tests
- Response codes
- Error messages
- Execution time
7. Advanced Features in Postman
1. Assertions: Postman provides several built-in assertions to verify different aspects of the API response (e.g., status code, headers, body content). 2. Data-Driven Testing: Use data files like CSV or JSON to run the same test with different inputs. 3. Mocking: Mock API responses to enable testing without relying on a live backend server. 4. Scripting: Use Javascript to write custom logic and logic for complex test scenarios.
Postman is a powerful platform with a wide range of features. By utilizing these features, you can effectively execute API tests to improve API quality and ensure a smooth integration process.