29

I'm using Postman to make REST API calls to a server. I want to make the name field dynamic so I can run the request with a unique name every time.

{
  "location":
  {
    "name": "Testuser2", // this should be unique, eg. Testuser3, Testuser4, etc
    "branding_domain_id": "52f9f8e2-72b7-0029-2dfa-84729e59dfee",
    "parent_id": "52f9f8e2-731f-b2e1-2dfa-e901218d03d9"
  }

}
MisterJames
  • 3,306
  • 1
  • 30
  • 48
manoj
  • 291
  • 1
  • 3
  • 6

6 Answers6

33

In Postman you want to use Dynamic Variables.

The JSON you post would look like this:

{
  "location":
  {
    "name": "{{$guid}}", 
    "branding_domain_id": "52f9f8e2-72b7-0029-2dfa-84729e59dfee",
    "parent_id": "52f9f8e2-731f-b2e1-2dfa-e901218d03d9"
  }

}

Note that this will give you a GUID (you also have the option to use ints or timestamps) and I'm not currently aware of a way to inject strings (say, from a test file or a data generation utility).

rajeshmag
  • 121
  • 11
MisterJames
  • 3,306
  • 1
  • 30
  • 48
  • 3
    The aforementioned "Dynamic Variables" link doesn't point directly at the info. Try [this link](https://www.getpostman.com/docs/v6/postman/environments_and_globals/variables) instead. – Mass Dot Net Mar 22 '18 at 14:31
  • Postman now has a subset of the fakerjs module so you can use `{{$randomFullName}}` in the response body. This is also the link to use https://learning.postman.com/docs/postman/scripts/postman-sandbox-api-reference/#dynamic-variables – Danny Dainton Feb 27 '20 at 08:48
  • @Samir: They keep changing the URLs! Here's the latest link to the Postman Learning article about "Dynamic Variables": https://learning.postman.com/docs/postman/variables-and-environments/variables/#using-dynamic-variables – Mass Dot Net Apr 28 '20 at 20:29
  • Is that any way to make sure always get different random values, I used {{$randomNoun}} to avoid name conflict, but still getting a NAME CONFLICT error in my POST... Looking solution for avoiding that issue.. – greenridinghood May 25 '23 at 19:41
16

In Postman you can pass random integer which ranges from 0 to 1000, in your data you can use it as

{
  "location":
  {
    "name": "Testuser{{$randomInt}}",
    "branding_domain_id": "52f9f8e2-72b7-0029-2dfa-84729e59dfee",
    "parent_id": "52f9f8e2-731f-b2e1-2dfa-e901218d03d9"
  }

}
Pratik Charwad
  • 697
  • 10
  • 19
15

Just my 5 cents to this matter. When using randomInt there is always a chance that the number might eventually be present in the DB which can cause issues. Solution (for me at least) is to use $timestamp instead.

Example:

{
    "username": "test{{$timestamp}}",
    "password": "test"
}
Goran.it
  • 5,991
  • 2
  • 23
  • 25
1

For anyone who's about to downvote me this post was made before the discussion in comments with the OP (see below). I'm leaving it in place so the comment from the OP which eventually described what he needs isn't removed from the question.


From what I understand you're looking for, here's a basic solution. It's assuming that:

  • you're developing some kind of script where you need test data
  • the name field should be unique each time it's run

If your question was more specific then I'd be able to give you a more specific answer, but this is the best I can do from what's there right now.


var counter = location.hash ? parseInt(location.hash.slice(1)) : 1; // get a unique counter from the URL
var unique_name = 'Testuser' + counter; // create a unique name
location.hash = ++counter; // increase the counter by 1

You can forcibly change the counter by looking in the address bar and changing the URL from ending in #1 to #5, etc.

You can then use the variable name when you build your object:

var location = {
    name: unique_name,
    branding_domain_id: 'however-you-currently-get-it',
    parent_id: 'however-you-currently-get-it'
};
Joe
  • 15,669
  • 4
  • 48
  • 83
  • Hi joe, thank you for your help you are right I used random name field but in json format. Because I work on postman and here I used json format, So can I use your code with my script in postman? – manoj Sep 22 '14 at 13:14
  • @manoj I have no idea, I've never even heard of Postman before :-) JSON is a data-storage language, it can't do programming of any kind. I assumed you were using Javascript since you mentioned JSON without any context, but clearly you're not. You probably can't use any of the answer, although if you'd tagged your question as Postman or just mentioned that you were using it, that would have saved us both the time :-) – Joe Sep 22 '14 at 13:18
  • @manoj I've updated your question for you to be more specific for what you need, and tagged it properly for you – Joe Sep 22 '14 at 13:25
0

Add the below text in pre-req:

var myUUID = require('uuid').v4(); pm.environment.set('myUUID', myUUID);

and use the myUUID wherever you want like

name: "{{myUUID}}"

It will generate a random unique GUID for every request

0
var uuid = require('uuid');
pm.globals.set('unique_name', 'testuser' + uuid.v4());

add above code to the pre-request tab. this was you can reuse the unique name for subsequent api calls.

Dynamic variable like randomInt, or guid is dynamic ie : you donot know what was send in the request. there is no way to refer it again, unless it is send back in response. even if you store it in a variable,it will still be dynamic

another way is :

var allowed = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
var shuffled_unique_str = allowed.split('').sort(function(){return 0.5-Math.random()}).join('');

courtsey refer this link for more options

arvin_v_s
  • 1,036
  • 1
  • 12
  • 18