在Postman中创建动态名称文本字段

man*_*noj 21 json postman

我正在使用Postman对服务器进行REST API调用.我想使名称字段动态化,这样我每次都可以使用唯一的名称运行请求.

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

}
Run Code Online (Sandbox Code Playgroud)

Mis*_*mes 24

在Postman中,您想要使用动态变量.

您发布的JSON将如下所示:

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

}
Run Code Online (Sandbox Code Playgroud)

请注意,这将为您提供一个GUID(您还可以选择使用整数或时间戳),我目前还没有注意到一种注入字符串的方法(例如,来自测试文件或数据生成实用程序).

  • 前面提到的“动态变量”链接并不直接指向信息。请尝试 [此链接](https://www.getpostman.com/docs/v6/postman/environments_and_globals/variables)。 (3认同)

Gor*_*.it 14

这件事我只需5美分.使用randomInt时,数据最终可能最终存在于数据库中,这可能会导致问题.解决方案(至少对我而言)是使用$ timestamp.

例:

{
    "username": "test{{$timestamp}}",
    "password": "test"
}
Run Code Online (Sandbox Code Playgroud)


Pra*_*wad 11

在Postman中,您可以传递随机整数,范围从0到1000,您可以在数据中使用它

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

}
Run Code Online (Sandbox Code Playgroud)


Joe*_*Joe 1

对于任何想要对我投反对票的人,这篇文章是在与 OP 进行评论讨论之前发布的(见下文)。我将其保留在适当的位置,以便最终描述他需要什么的OP的评论不会从问题中删除。


据我了解您正在寻找,这是一个基本的解决方案。假设:

  • 您正在开发某种需要测试数据的脚本
  • name每次运行时该字段都应该是唯一的

如果您的问题更具体,那么我可以给您更具体的答案,但这是我现在能做的最好的事情。


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
Run Code Online (Sandbox Code Playgroud)

#1您可以通过查看地址栏并将 URL 从结尾更改为#5等来强制更改计数器。

name然后,您可以在构建对象时使用该变量:

var location = {
    name: unique_name,
    branding_domain_id: 'however-you-currently-get-it',
    parent_id: 'however-you-currently-get-it'
};
Run Code Online (Sandbox Code Playgroud)