In the last tutorial, we have discussed GUID in general and how it has affected the overall internet mechanism of saving the data into the database. It has proved to be a satisfying and revolutionizing solution to a problem that existed for some time. We discussed that the most important reason for using GUID is that the collision rate is next to impossible while using GUID. Moreover, database transition becomes smooth and server functioning easy. Now that we are familiar with the idea of GUID, we will implement it practically in this tutorial.

Pre-Requisites:

  • Knowledge of GUID - What is GUID and structure of GUID (Refer the tutorial).
  • Knowledge of Environment and Variables in Postman - What are environments and variables in Postman (Refer the tutorial).
  • Knowledge of Pre-request script - What are pre-request scripts in Postman (Refer the tutorial).

What are dynamic variables?

In computer science, there are two kinds of variables namely static and dynamic.

Static variables are those variables that are assigned memory before the execution of the program. For example, when we declare a variable like var name; we are telling the operating system to reserve the memory for the variable name. If you use a variable without declaring it like name = "Harish", the computer will get confused. There is no memory assigned to such a variable so the computer thinks no such variable exists and hence it throws errors.

Another type of variable is dynamic variables. Dynamic variables, unlike static variables, are assigned the memory when the program is run and not beforehand. That is why they are named as dynamic. This can happen in a dynamic programming language such as Perl. Here you can determine one variable without specifying its type and use it anywhere in across programs.

Postman offers dynamic variable support. One such variable is GUID. While using GUID in Postman, you do not need to specify the memory or value of the variable specifically. Just specify guid in double curly braces (shown in next sections) and Postman will take care of the rest. There are other variables such as timestamp in Postman and, all those work similarly as GUID. Let's see how GUID is implemented in Postman.

GUID in Postman

GUID, as we have discussed above, comes in the category of dynamic variables. It does not need predefined memory usage or declaration.

In this tutorial, we will show you how to generate GUID in Postman and how to pass it as a header. We are not using any actual request for this because we are not analyzing the response here. The purpose here to understand How to create dynamic GUID? which can be used later either in the request URL, body or header. So in this section of the tutorial, we will be moving step by step covering the following:

  • Using dynamic GUID provided by Postman
    • GUID in URL
    • GUID in Body
    • GUID in Header
  • Generating GUID manually in Postman
    • Generating GUID in Postman
    • Saving GUID to an Environment Variable

How to use dynamic GUID in Postman?

GUID in postman can be used directly without writing any code. The main use of GUID is in URLs and Headers in Postman directly. Since through these two things you can map your data to the database. For example, you can pass the GUID in the URL to fetch the data associated with that key. The same can also be done by sending GUID inside headers. Where you are sending GUID depends totally on the type of request you are dealing with. It can require GUID to be in header, body or code.

How to use GUID as a Query Parameter in Postman?

GUID can also be used in URL without any initialization or any prior processes. You can directly write this variable in the request and it will work smoothly. This is shown below.

GUID in Postman Request

Yes, this is this simple. Since this mechanism is provided by Postman itself, no need to do anything else. Just pass it as variable and make use of it.

How to use GUID as Request Body in Postman?

In Postman, you can implement GUIDs directly into the Request Body by selecting the appropriate format of the body. The same is shown below for the request type as JSON.

GUID in Postman Body

How to use GUID as Header in Postman?

In Postman, GUID can be used in Header as shown below:

GUID in Header

The header will contain the actual value of GUID in the request.

Manually Generating GUID in Postman

Using GUID in pre-request scripts is not that mainstream. There are fewer uses of using GUIDs in the script as it will give you different value every time which will either be ultimately mapped to headers or queries of databases.

How to manually generate GUID in Postman?

For this, enter the web address www.google.com in the address bar.

Google_Endpoint

Go to Pre-Request Script in Postman.

Pre_Request

Type the following code in the pre-request script:

  • var uuid = require('uuid'); - To load the uuid module of JS in the variable uuid. Require is used in JS to load a module.
  • var myUUID = uuid.v4(); - version 4 of UUID from the module loaded in the previous step is saved in variable myUUID.
  • console.log(myUUID); - This line is to print the value of myUUID on the console.

UUID code

Note: It is important to note that the variable with name guid cannot be used inside pre-request script in Postman. You have to use uuid for the same. Although guid can be used inside URL and Headers directly. Also, it is noteworthy that guid can directly be used as an environment variable and there is no need to set it as a variable separately.

Now we know that Postman has got the value of GUID. So, we will try to print it on the console. Open Console to see the requests that we will send.

Postman Console

Now, execute the request by pressing Send. Look at the console. It will show you the uuid value received.

UUID console

Please refer to the GUID tutorial to know more about the format of GUID that we have received here.

Important Note:

Dynamic variables do not work under Postman Sandbox. So, if you want to save the value of guid (using global variable format {{variable}}) using normal javascript, you will not be able to do so. This has also been shown below, as you can notice the error in the very first line of code.

Using Guid As A Variable

Now let us send the request again and compare the GUID we got in the last result to the GUID I got now.

Comparing GUIDs

You can see that the value is changing for every request that you send. This is the main reason it can be used as a key to saving data in the database.

This was about the pre-request scripts. We will now see how GUID can be used inside headers and URLs in Postman.

How to save GUID in an Environment Variable in Postman?

Now that we have generated the GUID in Postman, we can use it directly in the headers or save it in the environment variable. Although you should know that GUID is predefined global variable in Postman and therefore there is no need to save it explicitly. But, requests may demand anything and you should be prepared for that.

To save the GUID we generated, recall our lessons from Environment and Variables tutorial. You can use the following code to save the GUID:

Select an Environment from the panel (or create one if does not exist).

GUID Environment

Type the following in the pre-request script along with the code we wrote in the previous section:

pm.environment.set("myGUID", myUUID);

Setting GUID Variable

Note: This has already been discussed in the environment and variable tutorial. Here, we are setting a new environment variable called myGUID and saving the myUUID value into it.

Now execute the request again and see the environment variables by clicking the eye icon.

myGUID Variable

As you can see, your variable is now saved and can be used as a normal variable.Which is like {{myGUID}}.

GUID in postman can also be used directly as I mentioned above. It is a predefined global variable whose value need not be initialized. Postman gives us great convenience in just mentioning the name and use GUID wherever necessary and allowed.

GUID is a very useful and important part of the Postman and API world. What else is required when you are getting a different value every time without using any code actually? GUID helps a lot in associating data into the database and other storages and saving this as a key. It helps in migrating the data to another server or merging two databases as these GUID values will always be different throughout each and every database in the world. If you have a good practice on SQL, then you can always try experimenting. For this tutorial, this was all from my side. Keep learning. Keep practicing.

GUID - Global Unique Identifiers
GUID - Global Unique Identifiers
Previous Article
API Documentation in Postman
API Documentation in Postman
Next Article
Harish Rajora
I am a computer science engineer. I love to keep growing as the technological world grows. I feel there is no powerful tool than a computer to change the world in any way. Apart from my field of study, I like reading books a lot and developing new stuff.
Reviewers
Lakshay Sharma's Photo
Lakshay Sharma

Similar Articles

Feedback