json 在 Postman 中制作动态名称文本字段
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/25973797/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Make dynamic name text field in Postman
提问by manoj
I'm using Postmanto 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.
我正在使用Postman对服务器进行 REST API 调用。我想让 name 字段动态化,以便我每次都可以使用唯一的名称运行请求。
{
"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"
}
}
回答by MisterJames
In Postman you want to use Dynamic Variables.
在 Postman 中,您想使用Dynamic Variables。
The JSON you post would look like this:
您发布的 JSON 如下所示:
{
"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).
请注意,这将为您提供一个 GUID(您还可以选择使用整数或时间戳),而且我目前不知道注入字符串的方法(例如,从测试文件或数据生成实用程序)。
回答by Goran.it
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.
这件事只需要我 5 美分。使用 randomInt 时,数字最终可能会出现在数据库中,这可能会导致问题。解决方案(至少对我而言)是使用 $timestamp 代替。
Example:
例子:
{
"username": "test{{$timestamp}}",
"password": "test"
}
回答by Pratik Charwad
In Postman you can pass random integer which ranges from 0 to 1000, in your data you can use it as
在 Postman 中,您可以传递范围从 0 到 1000 的随机整数,在您的数据中,您可以将其用作
{
"location":
{
"name": "Testuser{{$randomInt}}",
"branding_domain_id": "52f9f8e2-72b7-0029-2dfa-84729e59dfee",
"parent_id": "52f9f8e2-731f-b2e1-2dfa-e901218d03d9"
}
}
回答by Joe
For anyone who's about to downvote methis 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.
对于任何要对我投反对票的人,这篇文章是在与 OP 的评论讨论之前发表的(见下文)。我把它留在原地,所以来自 OP 的评论最终描述了他需要什么并没有从问题中删除。
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
namefield should be unique each time it's run
- 您正在开发某种需要测试数据的脚本
name每次运行时该字段都应该是唯一的
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 #1to #5, etc.
您可以强行通过查看地址栏,并在结束更改URL改变计数器#1对#5等
You can then use the variable namewhen you build your object:
然后,您可以name在构建对象时使用该变量:
var location = {
name: unique_name,
branding_domain_id: 'however-you-currently-get-it',
parent_id: 'however-you-currently-get-it'
};
回答by Chhavi Beri
Add the below text in pre-req:
在 pre-req 中添加以下文本:
var myUUID = require('uuid').v4(); pm.environment.set('myUUID', myUUID);
var myUUID = require('uuid').v4(); pm.environment.set('myUUID', myUUID);
and use the myUUID wherever you want like
并在任何你想要的地方使用 myUUID
name: "{{myUUID}}"
名称:“{{myUUID}}”
It will generate a random unique GUID for every request
它将为每个请求生成一个随机的唯一 GUID
回答by arvin_v_s
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.
将上述代码添加到预请求选项卡中。这是您可以为后续 api 调用重用唯一名称。
Dynamic variablelike 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
像 randomInt 或 guid 这样的动态变量是动态的,即:您不知道请求中发送了什么。没有办法再次引用它,除非它被发回响应。即使你将它存储在一个变量中,它仍然是动态的
another way is :
另一种方法是:
var allowed = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
var shuffled_unique_str = allowed.split('').sort(function(){return 0.5-Math.random()}).join('');
courtsey refer this linkfor more options
法庭请参阅此链接以获取更多选项

