Tosca tester
Postman API testing + installation tutorial and request creation
APIs are an important aspect of software development because they allow interaction and data exchange between different software components. Thanks to APIs, your application can communicate efficiently with other systems and provide users with accurate and up-to-date information. We can test the API functionality through several tools, in the following lines we will talk about how to do API testing in Postman tool. If you want to read more about APIs and testing them using another popular tool, check out our article on API testing in Cypress / Cypress API testing.
What is Postman?
Postman.com is an API testing tool used by more than 30 million users. It has gained immense popularity in the IT industry among developers and testers. Its simple and user-friendly interface is very useful in documenting, designing and testing APIs. With Postman, you can easily create requests to API endpoints, send different types of data and explore the responses effortlessly. It is also designed for less experienced beginners who don’t want to delve into complex code.
Why use Postman for API testing?
- Intuitive interface and use: Postman stands out with its intuitive interface and quick access to functionalities, making it an excellent tool when working with APIs. With Postman, you can easily create collections and requests, and its clean interface makes it easy to keep track of your projects.
- Compatibility – Postman is available on macOS, Windows, Linux and Chrome. It works on Windows 7 and later, and can also be used on Windows for ARM devices using the ia32 binary. On the Linux platform, it supports Ubuntu 12.04 and later, Fedora 21, and Debian 8. It supports many authentication mechanisms such as basic authentication, OAuth, OAuth2, Jwt, etc.
- Automation and integration: Postman enables automation of API testing and its integration into the CI CD pipeline. In addition, tests are written in JavaScript, which provides a wide range of possibilities for creating complex test logic.
- Sharing and collaboration: Postman allows you to share collections and test environments with other team members. This simplifies collaboration and allows for better organization of testing within the team.
- Support – Postman has a strong support and community that contributes to improving its use on various platforms such as Git (read more on Git and DevOps), Stackoverflow and others. Postman Discourse, blogs, Roadman, and the support center are available exclusively to support Postman.
- Price (postman pricing) – Postman is open-source and therefore free. All of the above benefits are free of charge. In addition, there are paid Postman Pro and Postman Enterprise plans that are suitable for small and large businesses.
Postman installation
You can install Postman as an app, and it’s also available in a web version. This tutorial uses desktop version 10.24.22. Link for free installation can be found here.
Create your first Postman workspace
In Postman, you can create workspaces or workspaces at different levels of privacy in order to organize and manage projects and tests. Personal workspaces are only visible to you. Team workspaces can in turn be created in order to share individual collections with other team members. This option simplifies collaboration and keeps track of testing within the team.
How to create a workspace?
- Click on “Workspaces” at the top of the home page.
- Click on the “Create Workspace” button.
- Give your workspace a name. Set visibility to private or public workspace. If you choose public, make sure your public profile is enabled as well. This way you can collaborate with anyone publicly. If you’re part of a team, you can restrict workspace sharing to certain members only.
- Finally, confirm the creation of the new workspace by clicking the “Create” button and your first workspace is ready!
Postman environment and its settings
Setting up the environment in Postman is important for several reasons. One of them is the ability to define variables that you can then use in different requests within the same environment. This means that you don’t have to create new variables for each individual request. Instead, you can simply change variable values in one place, saving you time and simplifying your work. In addition, using environments helps keep variables separate. This means that you minimize the risk of conflicts not only at the global level, but also in other test environments. This organized structure makes it easier for you to keep track of your projects.
In this case, a new environment called LocalEnvironment has been created. For this environment, a variable named base_url with the same Initial and Current value was stored.
Postman variables – use of variables
In programming, variables allow you to store and reuse values in code. This way you can reference the value in your collections, environments, and queries in Postman. And if you need to update a value, you only need to change it in one place. The base_url variable in Postman stores a base URL that defines the beginning of a path to resources on a web page or in an API. This variable serves as a reference point that is used to build the URL for each request in the collection. When the base_url variable is set, all other requests in the collection can use relative URLs that connect with the base_url to create a full path to the resources. When you add a variable into to the environment, you can specify what is called an Initial value and a Current value. The Initial value is also called a shared value, since it is synchronized with your account using Postman’s cloud servers. It is also published if you choose to make the workspace public along with the collection. If the value includes sensitive data such as a password or key, you can encrypt the value by selecting the secret variable type. Current value, otherwise known as local, is used when sending requests in your local instance of Postman. It is never synchronized with your account or shared with your team unless you choose to store it permanently.
Creating an API collection (Postman API collection)
The collection allows you to group requests with different types of methods (for example, GET, POST, DELETE, and PUT) and organize them into folders or subfolders. This allows you to share collections with team members, as well as import and export them to other Postman instances. In addition to simplifying your work environment, collections allow you to generate complete API documentation and automate the running of requests.
- To create a new collection, click on the “Collections” tab in the panel on the left. This tab is where you can manage all your collections.
- Then click “Create Collection”.
- When you click on the button to create a new collection, a dialog box will appear where you can enter a name and optional description for your collection (in this case it is TestCollection).
Congratulations! You managed to create your first collection in Postman. You can add and manage different requests, scripts, variables and other settings according to your needs.
Adding a request (Postman request) to the collection
Collections allow you to group multiple requirements together, simplifying the management and execution of test scenarios.
- Click on the name of the collection within which you want to add a new request.
- When you click the “Add Request” button, a dialog box will appear where you can enter a name for the new request. It is also advisable to add a description that clearly describes the purpose of this request.
- Once you open a new panel, you can specify the details you need for your request. You will need to know the URL and method. You can also specify optional values such as authorization, parameters, and data body and headers.
Once you’ve decided on a workspace, it’s time to choose a request method. For the first request method, choose GET.
Parameters in API requests are the key components that allow you to specify the exact details and settings for each request. For example, parameters in the GET method of a request are used to transfer data from the client to the server via a URL. These parameters are included in the Query String, which is the part of the URL after the question mark. You can use these parameters to, for example, filter or specify the data you want to retrieve from the server.
Authorization – Postman supports multiple authorization and authentication methods such as basic authentication, API key, Bearer token, OAuth, etc. Headers – Headers are important when communicating with APIs because they provide the necessary contextual information for the server that is processing the request. In Postman, you can add and edit these headers directly in the UI, allowing you to easily configure requests and test different API communication scenarios. Body – The body of an API request in Postman is used to carry the data or information you want to send to the server. This data can be in the form of different formats such as JSON, XML, HTML or text. For example, if you want to create a new record in a database, you can send data containing information about that record in the form of a request body.
4. When you have finished configuring the request, click the “Send” button. Postman will display the status code, headers, and body of the response.
How to analyze the response from the server?
Now it’s time to analyze the response from the server to make sure it matches the expected result. Postman provides different views such as Pretty, Raw and Preview to help you analyze the data efficiently. HTTP API responses in the 200 range indicate successful API requests. If the response is in the range of 200, it indicates that the server has accepted and processed the client request without any errors. This confirms the successful completion of the API request. A range of 400 indicates that the server understood the client request, but there is a problem with the request itself. A range of 500 indicates server errors.
Conclusion
Manual testing can quickly turn into a lengthy process with the added risk of errors. Postman’s graphical user interface is already a necessity in every API developer’s arsenal. I hope this tutorial will help you get started exploring the potential that Postman offers when testing APIs.