Skip to content

How To Test Web Service Using Postman

API Testing Blog

A Comprehensive Guide to Testing Web Services with Postman

Postman is a powerful tool for testing web services (APIs). It allows you to send requests, receive responses, and analyze the results, making it an essential tool for any developer or tester involved in API development. This guide will walk you through the fundamentals of using Postman for API testing.

1. Installation and Setup

Before diving into testing, you’ll need to install Postman. You can download the latest version for your operating system from the official website https://www.postman.com/. Once installed, you can start using Postman by creating a new workspace or importing existing ones.

2. Sending Your First Request

To interact with a web service, you need to send requests. Postman provides a user-friendly interface to craft and send these requests.

2.1. Creating a Request

  • Open a new request tab: Click the “New” button in the top left corner or press Ctrl+N (or Cmd+N on Mac).
  • Choose the request type: Select the HTTP method (e.g., GET, POST, PUT, DELETE) you wish to send.
  • Enter the request URL: Type the full URL of the API endpoint you want to access.
  • Add headers: Click on the “Headers” tab and add appropriate headers (key-value pairs). For example, Content-Type: application/json is a common header for JSON requests.

2.2. Sending the Request

  • Click “Send”: This will submit your request to the server.
  • Analyze the response: Postman displays the server’s response in the “Response” tab, including the status code, headers, and body content.

3. Testing with Different Request Types

API testing involves sending various types of requests to test different scenarios. Postman handles each request type effectively.

3.1. GET Request

A GET request retrieves data from the server.

Example:

URL: https://reqres.in/api/users
Method: GET
Headers:
Content-Type: application/json

This request will fetch data about users from the API endpoint https://reqres.in/.

3.2. POST Request

A POST request sends data to the server, often for creating new resources.

Example:

URL: https://reqres.in/api/users
Method: POST
Headers:
Content-Type: application/json
Body:
{
"name": "John Doe",
"job": "Tester"
}

This request sends data for creating a new user with the specified name and job.

3.3. PUT Request

A PUT request updates an existing resource on the server.

Example:

URL: https://reqres.in/api/users/2
Method: PUT
Headers:
Content-Type: application/json
Body:
{
"name": "Jane Doe",
"job": "Software Engineer"
}

This request modifies the user with the ID “2” to have the new information provided.

3.4. DELETE Request

A DELETE request removes a resource from the server.

Example:

URL: https://reqres.in/api/users/2
Method: DELETE

This request will delete the user with the ID “2” from the server.

4. Understanding Response Codes

The server’s response to a request includes a status code that indicates the outcome. Here are some common status codes:

  • 200 OK: Successful response, the request was processed successfully.
  • 201 Created: Indicates that a new resource has been created.
  • 400 Bad Request: The request is invalid.
  • 401 Unauthorized: The request requires authentication.
  • 404 Not Found: The resource requested does not exist.
  • 500 Internal Server Error: An error occurred on the server.

5. Using Assertions for Verification

Postman allows you to write assertions to verify different aspects of the response, ensuring that the API behaves as expected.

5.1. Test Tab

Go to the “Tests” tab in the request editor to add assertions.

5.2. Sample Assertions

  • Checking the status code: pm.test("Status code is 200", function () {pm.response.to.have.status(200);});
  • Checking the response body content: pm.test("Response body contains 'success'", function () {pm.expect(pm.response.text()).to.include('success');});
  • Checking the response headers: pm.test("Response has Content-Type header", function () {pm.response.to.have.header('Content-Type');});

6. Utilizing Environment Variables

Environment variables enhance reusability and modularity in your tests.

6.1. Managing Environments

  • Go to “Manage Environments” from the settings menu.
  • Create an environment with relevant key-value pairs.
  • Example: baseUrl, token, userId.

6.2. Using Variables

  • Use ${} syntax to access environment variables in your requests and assertions.

Example:

URL: {{baseUrl}}/api/users
Headers:
Authorization: Bearer {{token}}

This will substitute the baseUrl and token values from the selected environment.

7. Leveraging Collections

Collections in Postman help you organize and group related requests together for streamlined testing.

7.1. Creating a Collection

  • Click the “New” button and select “Collection.”
  • Give your collection a name and description.

7.2. Adding Requests to Collections

  • Click the ”+” icon next to the collection name and select “Add Request.”
  • Drag and drop requests from the workspace into the collection.

7.3. Running Collections

  • Click the “Run” button next to the collection name.
  • Select the environment and choose “Run All” or “Run Specific Requests.”

8. Exploring Advanced Testing Features

Postman offers advanced capabilities to further enhance your API testing process:

  • Mock Servers: Simulate API responses for testing scenarios even before the actual API is ready.
  • Data-Driven Testing: Execute requests with different inputs from a source like a CSV file.
  • Scripts: Write custom JavaScript code to automate complex testing logic.
  • Integrations: Connect Postman with other tools like GitHub, CI/CD pipelines, and more.

By mastering the concepts and techniques detailed in this guide, you are well-equipped to utilize Postman efficiently for comprehensive web service testing. Remember that continuous exploration and practice are key to unlocking the full potential of this versatile tool for your API testing journey.

API Testing Blog