在邮递员中为 API 测试创建唯一的正文请求 [英] Create unique body request in postman for API-test

查看:53
本文介绍了在邮递员中为 API 测试创建唯一的正文请求的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在请求正文中使用唯一的电子邮件属性在邮递员上创建测试请求.

I want to create test request on postman with unique email property in the request body.

{
    ...
    "email": "{{email_string}}.com",
    ...
}

我已经在环境中使用静态字符串设置了 email_string,但是有什么方法可以在请求发生之前动态设置 email_string 吗?

I've set email_string with static string in enviroment, but is there any way that I can set email_string dynamically before request occured?

推荐答案

作为上一个答案的替代方案,您可以使用 sendRequest 函数从设计的 3rd 方 API 获取值返回随机数据.

As an alternative to the previous answer, you could use the sendRequest function to get the value from a 3rd party API that is designed to return randomised data.

这可以添加到Pre-Request Script标签:

pm.sendRequest("https://randomuser.me/api/", (err, res) => {
    // Get the random value from the response and store it as a variable
    var email = res.json().results[0].email
    // Save the value as an environment variable to use in the body of the request
    pm.environment.set("email_address", JSON.stringify(email))
})

可以使用此 API 创建大量随机数据,但它是第 3 方 API,因此您无法控制此更改.如果你只是在短期内需要这个,我相信它会很好.

You could potentially create lots of randomised data using this API but it is a 3rd party API so you won't have any control over this changing. If you only need this in the short term, i'm sure it will be fine.

还有一点值得记住的是 Postman 内置了 Lodash,这样你就可以使用任何模块功能,减少一些原生 JS 代码.

Something also worth remembering is that Postman comes with Lodash built-in so that gives you the ability to use any of that modules functions, to reduce down some of the native JS code.

这篇关于在邮递员中为 API 测试创建唯一的正文请求的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆