In This Tutorial, we will Cover What Are Postman Collections, How to Import and Export Collections into and from Postman and How to Generate Code Samples in Various Supported Languages Using Existing Postman Scripts:
These are some of the really powerful features that make Postman a tool of choice for nearly all the API developers and testers.
=> Visit Here For The Exclusive Postman Training Tutorial Series.
What You Will Learn:
What Is A Postman Collection?
Postman collection is nothing but a container or folder for storing Postman requests. In simple terms, its an aggregation of Postman requests. Collections play an important role in organizing requests belonging to the same application etc.
For example, if you are testing or validating a restful API that has 10 endpoints. Then, it makes sense to organize them in a collection that would make things like applying collection variables, import/export easier and could be run as part of a single collection.
Here is a Video Tutorial:
A Collection enables a user to:
#1) Run all requests at once.
#2) Have collection level variables set that can apply to all requests within that collection. For example, instead of individually adding headers to each request, you can simply apply headers to all requests within that Postman collection using pre-request scripts or authorization headers.
#3) Collections can be shared with other users as JSON or through URLs as hosted collections on the Postman provided server.
#4) Execute common tests for all requests belonging to a collection. For example, if you need to check the status code for each request in the collection as HTTP 200, then instead of adding this test to all the individual requests, you can simply add it all at the collection level and it would apply to all requests when the collection is executed.
Creating Postman Collections
Here is how you can create an empty collection and add multiple requests as a part of the same collection:
#1) Create a new empty collection.
#2) Add collection description and name.
#3) To add new requests to the collection, click on Collection and click Add requests (please note it’s also possible to create a request first and then add it to the collection as well as moving in requests from one collection to another).
Exporting/Importing A Postman Collection
Now let’s see how we can actually import or export a Postman collection in Postman. First, let’s create a sample Postman collection in Postman with say 4-5 requests.
It’s important to understand here that Postman collection can be exported as a JSON file and can be easily shared with someone with whom we intend to.
Similarly importing a collection is as simple as importing a JSON file that will show up as the request collection in your Postman application.
For the sake of illustration, we will use an already hosted collection here.
If you download this file, you can see that it’s a file in JSON format. This is as good as a Postman collection exported to the Postman collection 2.1 format.
We will see how we can import this JSON file as a Postman collection in the application and export it back and share it as JSON.
#1) To import a collection, download the above file and save it as a file to the file system.
You can simply download the JSON file using Curl command as below
curl https://raw.githubusercontent.com/Blazemeter/taurus/master/examples/functional/postman-sample-collection.json --output sample-postman-collection.json
#2) Now open Postman and click Import.
#3) Select the downloaded JSON file. Once the selection is complete, you can see that the JSON file gets imported as a Postman collection in the application.
#4) You can now browse through the various requests that are available in the collection.
#5) Export back the collection to JSON format (so that it can be shared with others). For example, you add one more request to this collection and click export. The resultant JSON file for collection will now contain the newly added request as well.
#6) Click “…” icon/button near the collection name to see the menu with options and click Export.
#7) Select the Collectionv2.1 format for export option (We will see the differences between these two file types in the later tutorials).
Executing Postman Collections
Let’s see how can we execute individual requests inside a collection and run all the requests in an entire collection using a collection runner.
To run an individual request, simply open any particular request from the collection and click the “SEND” button to execute that request.
In order to run an entire collection i.e. all the requests present in the given collection, you need to click the “play” button just beside collection in Postman and select the “Run” option to open the collection runner and execute the entire collection with the given collection configuration.
Please refer to the screenshots below.
Postman does provide a lot of flexibility, in terms of selecting the Run Configuration for collection.
For Example, you can choose what environment file should be referred to while running the collection. If in case there is data configuration being used by the Postman requests, then we can simply provide a data file before running the collection.
In the below image, we can see the execution results/summary for the selected collection. It gives a summarized view of whatever was executed and what the results were.
Exporting Postman Request As Code
Now let’s see how can we simply export an existing Postman collection into code/script in one of our favorite programming languages (Postman supports a lot of formats out of the box, as a result of which, you could download/export an existing request into multiple formats and use it as desired).
To export an existing request as code, open the request and click the “Code” link just beneath the request URL.
This will open a window with the default cURL script selected and the request will be displayed in the form of a cURL script. Depending on what different formats you choose, the request text will change accordingly and the same can be copied over and used as desired.
Importing Postman Request From Code
Similar to export, we can also import a request in various formats into a Postman collection.
We will demonstrate this using a cURL request that will be changed to a Postman request through import functionality. To import a request, simply click “Import” in the top left corner in Postman and wait for the dialog window where you need to select the “Paste Raw Text” option to open.
Now you can simply paste the cURL URL here and once the “Import” button gets clicked, the request should be created in Postman with different fields substituted against their values as per the request supplied.
In this tutorial, we learned about Postman collections which is an extremely important building block in Postman application.
Collection is an important core component of Postman that allows you to manage and maintain requests clearly and provide a lot of other features like sharing collections, executing entire collections, adding common properties such as Auth header to all requests belonging to a particular collection and so on and so forth.
We also touched upon how to export an existing request as different language bindings, and how to import an existing script to Postman’s request.
In our upcoming tutorial, we will see how these building blocks can be utilized for even complex and cumbersome API flows and allow us to manage the requests properly and execute them on demand.
=> Check ALL Postman Tutorials Here.