What is {{$guid}} used for in Postman?
Asked Answered
D

5

102

Postman's official website states that Postman has a few dynamic variables. My question is about:

{{$guid}}: Adds a v4 style guid

What kind of variable is {{$guid}}? How can it be used in test scripts for API requests?

Durative answered 16/6, 2016 at 6:24 Comment(2)
And why v4 ? Is there a v3? Where can I find documentation about Guid. Is it an ISO or other standard?Nomadic
Yes, there are multiple versions. See guid.one.Versicolor
C
135

GUID is the acronym for "Globally Unique Identifier". A GUID is mainly used to produce hexadecimal digits with groups separated by hyphens for uniqueness purposes, for example:

b3d27f9b-d21d-327c-164e-7fb6776f87b0

In postman you can use this to generate and send a random GUID to your api as required:

{
    "id": "{{$guid}}",
}

On Send would produce(with the random example above):

{
    "id": "b3d27f9b-d21d-327c-164e-7fb6776f87b0",
}
Chrissa answered 22/7, 2016 at 20:22 Comment(3)
Note that you might want to use "${{$randomUUID}}" if you need it to change at each request.Variolous
{{$guid}} does change at every requestCartoon
@Variolous Just be aware that you don't need the $ prefix; it should just be {{$randomUUID}}Toponym
S
33

In case you are looking to generate a V4 guid which you want to set as an environment variable, which can then be used across your collection, you may do something like this in your pre-request script:

var uuid = require('uuid');
postman.setEnvironmentVariable('guid', uuid.v4());

You may then use the environment variable guid across multiple calls in your collection. This becomes useful when you want to generate a guid once for an entire collection and need it to be constant across multiple requests.

If you want the guid to be generated for every request, you may directly use {{$guid}} in your payload like the other answers have explained.

Based on this interesting answer by Osloan in github: https://github.com/postmanlabs/postman-app-support/issues/886

Straightway answered 30/10, 2018 at 5:31 Comment(2)
How does this differ from the built-in $guid in the Question?Supermarket
$guid in the Question will generate a new guid each time. This answer is to help people who want the guid to stay constant across multiple requests.Straightway
G
5

Use {{$randomUUID}} like variable to change at each request.

Georgiegeorgina answered 14/1, 2022 at 8:39 Comment(0)
P
1

For random generator use the below code in pre-Request

var text="shipment";
var charset = "abcdefghijklmnopqrstuvwxyz0123456789";
    for( var i=0; i < 8; i++ )
        text += charset.charAt(Math.floor(Math.random() * charset.length));    
postman.setEnvironmentVariable("awb", text);

for detailed explanation follow below link

http://jmeterblogb.blogspot.in/2016/10/how-to-automate-rest-api-in-postman.html

Petrol answered 25/10, 2016 at 14:12 Comment(2)
Saving guid to a global variable is not working in PM. I used this code to generate a random string and save it to a variable. Thanks!Metralgia
Great for generating random text - non guidJabin
A
1

In Postman there are Two types of variable available.

1). If you have static variable like Ip, Port or Something who doesn't change through the project you can store into the Environments Variable using

1.1). Setting > Manage Environment > Add > Name of Environment > Add Parameters Like >In Key : Port and In Value : 80

1.2). You can also add in request > Pre-request Script

add "Set an environment variable " from snippet...

postman.setEnvironmentVariable("Port", "80");

2). for Dynamic Variable like SessionIdentifier.You have to capture from response and add in Test set a global variable from snippet it looks like

"var jsonData = JSON.parse(responseBody); postman.setGlobalVariable("SessionIdentifier", jsonData.Data.);"

in the same response. For use, you should {{Port}},{{SessionIdentifier}}.

After it added into the Environments.

Anschluss answered 5/7, 2017 at 5:23 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.