27
votes

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"
  }

}
6
and your question is? - Leo
really whats your question? - Pramod S. Nikam
My question is I want to make "name" field dynamic so when ever I run this script its running successfully instead of error "name is already taken" - manoj
so you need a code to create the above json data with the name field dynamically (randomly) generated? ok what have you tried so far? - Yazan
@yazan I have no idea about this how I can generate randomly data? I nothing to tried over random data. So please suggest me how I can solve this? - manoj

6 Answers

32
votes

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).

15
votes

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"
}
13
votes

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"
  }

}
1
votes

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'
};
0
votes

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
votes
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