简体   繁体   English

Postman - 指导

[英]Postman - GUIDS

As part of my API call I need to pass in random ids in the body, currently I am creating the IDs using GUID in C# but its proving to be really time consuming as I need to test APIs in large batches.作为我的 API 调用的一部分,我需要在正文中传递随机 ID,目前我正在使用 C# 中的 GUID 创建 ID,但事实证明它非常耗时,因为我需要大批量测试 API。 I am trying to figure out if it's possible to create a GUID in Postman on the fly and save them into a variable and pass it into require parameters through the API call.我试图弄清楚是否可以在 Postman 中动态创建 GUID 并将它们保存到变量中并通过 API 调用将其传递给所需参数。

I came across few resources like我遇到了一些资源,例如

  1. What is {{$guid}} used for in Postman? Postman 中使用的 {{$guid}} 是什么?

  2. https://www.toolsqa.com/postman/guid-in-postman/ https://www.toolsqa.com/postman/guid-in-postman/

First issue I am having is that I want to create the ID without the dash, so instead of b3d27f9b-d21d-327c-164e-7fb6776f87b0 I want b3d27f9bd21d327c164e7fb6776f87b0 .我遇到的第一个问题是我想创建没有破折号的 ID,所以我想要b3d27f9b-d21d-327c-164e-7fb6776f87b0而不是b3d27f9bd21d327c164e7fb6776f87b0

Secondly, I want to save these into a environment variable and pass it where it's required eg其次,我想将这些保存到环境变量中并将其传递到需要的地方,例如

In body as raw text I need to pass:在正文中作为原始文本我需要通过:

{
    "clientID":{{id}},
    "clientpassword":{{password}},
}

In the 'Tests', I currently have this script but I can't manage to get it working.在“测试”中,我目前有这个脚本,但我无法让它工作。

let id = {{$guid}};
let password = {{$guid}};

I have created 2 global variables called id and password我创建了 2 个名为 id 和 password 的全局变量

Also, currently I have these in the 'Tests' section of the postman, I see there is also a pre-request script.另外,目前我在 postman 的“测试”部分中有这些,我看到还有一个预请求脚本。 In which should I put this?我应该把这个放在哪里?

EDIT Also, another thing I am trying to do is, sometimes I need to search for a user by providing a client id in different places.编辑另外,我想做的另一件事是,有时我需要通过在不同的地方提供客户 ID 来搜索用户。 Is it possible to put the id and password in one place where I can either update it manually or by running the guid, so that I don't need to update it in each API test I do but rather it should grab it from one place where I updated it.是否可以将 id 和密码放在一个可以手动更新或运行 guid 的地方,这样我就不需要在每个 API 测试中更新它,而是应该从一个地方获取它我更新它的地方。

You do something like this in the Pre-request Script sandbox:您在Pre-request Script沙箱中执行以下操作:

let idGuid = pm.variables.replaceIn('{{$guid}}')
let passwordGuid = pm.variables.replaceIn('{{$guid}}')

pm.variables.set("id" , idGuid.replace(/-/g, ''))
pm.variables.set("password" , passwordGuid.replace(/-/g, ''))

These would then resolve the variables that you have placed within the request body .然后,这些将解析您放置在request body中的变量。

Not the most efficient solution but it would create the values you would like.不是最有效的解决方案,但它会创造你想要的价值。

There are also a number of other fake data variables available for you to use in the application:还有许多其他的假数据变量可供您在应用程序中使用:

https://learning.getpostman.com/docs/postman/scripts/postman_sandbox_api_reference/#dynamic-variables https://learning.getpostman.com/docs/postman/scripts/postman_sandbox_api_reference/#dynamic-variables

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM