在 Postman 中使用 Run 进行编码
一些 API 发布者在他们自己的 API 文档旁边使用在 Postman 中运行按钮。例如,如果用户在开发人员门户中输入数据,则 Run in Postman API 可以将此提供的信息作为环境变量值动态注入到嵌入式 Run in Postman 按钮中。
Run in Postman API 使用该_pm()
方法通过现有的动态 Run in Postman 按钮在您网站的客户端代码中创建或更新环境。
作为另一个示例,您可以使用 API 将登录凭据传递给 Postman:
_pm('env.create', 'Spotify', {
user_id: 'spotifyuser',
authorization: 'Bearer 1234xyzd'
});
内容
创造新环境
使用env.create
方法创建新环境:
_pm('env.create', 'environment_name', {key: value}, runButtonIndex);
您不能用于
env.create
创建重复的环境。使用现有环境名称进行的调用将失败。
使用用户输入的 API 密钥创建新环境:
function () {
var stagingKey = document.getElementById('staging-key-input').value,
productionKey = document.getElementById('production-key-input').value,
runButtonIndex = 0,
envData = {
stagingKey: stagingKey,
productionKey: productionKey
};
_pm('env.create', 'API Keys', envData, runButtonIndex);
}
该env.create
操作将true
在成功或false
失败时返回。
编辑现有环境
使用env.assign
方法更新环境:
_pm('env.assign', 'environment_name', {key: new_value, new_key: value}, preventOveride, runButtonIndex)
该
env.assign
方法适用于您在创建时包含在 Run in Postman 按钮中的环境,或者您使用该env.create
方法添加的环境。您不能使用它env.assign
来创建新环境。env.assign
如果环境不存在,则使用调用将失败。
更新环境的 API 密钥:
function () {
var stagingKey = document.getElementById('staging-key-input').value,
productionKey = document.getElementById('production-key-input').value,
preventOveride = true;
runButtonIndex = 0,
envData = {
stagingKey: stagingKey,
productionKey: productionKey
};
_pm('env.assign', 'API Keys', envData, preventOveride, runButtonIndex);
}
该env.assign
操作将true
在成功或false
失败时返回。
替换现有环境
使用env.replace
方法替换整个环境:
_pm('env.replace', 'environment_name', {key: value}, runButtonIndex)
您不能使用
env.replace
来替换不存在的环境。
替换环境:
// Existing environment named 'user_data'
{
auth_token: 'q4yugoiwqu4habddef3897ryq3891s',
user_id: '823',
session_data: {}
}
// Replace the 'user_data' environment
_pm('env.replace', 'user_data', {});
该env.replace
方法将true
在成功或false
失败时返回。
删除现有环境
使用该env.remove
方法删除现有环境。
_pm('env.remove', 'environment_name', runButtonIndex)
要删除环境:
// Existing environment named 'user_data'
{
auth_token: 'q4yugoiwqu4habddef3897ryq3891s',
user_id: '823',
session_data: {}
}
// Remove the 'user_data' environment
_pm('env.remove', 'user_data');
该env.remove
方法将true
在成功或false
失败时返回。指定的环境必须存在,否则env.remove
将失败。
在不同的环境中使用多个按钮
您可以在单个页面上嵌入多个按钮。如果要在每个按钮中包含不同的环境,请启用该segregateEnvironments
属性。
_pm('_property.set', 'segregateEnvironments', true);
如果启用
segregateEnvironments
,则必须使用runButtonIndex
所有pm()
方法来根据每个按钮在页面DOM中的位置来引用它。因为segregateEnvironments
默认是禁用的,默认runButtonIndex
是可选的。
包括索引
如果启用segregateEnvironments
,则必须使用runButtonIndex
所有pm()
方法来根据每个按钮在页面DOM中的位置来引用它。是runButtonIndex
一个整数。
var runButtons = Array.prototype.slice.call(document.getElementsByClassName('postman-run-button')),
runButtonIndex = runButtons.indexOf(elem);
使用 jQuery 的索引
var runButtonIndex = $('postman-run-button').index(elem);
获取所有环境
您可以使用该get()
方法检索所有环境的数组。
_pm('_property.get', 'environments')
这将返回一组环境:
[
{
"button_index": 0,
"name": "env1",
"values": [
{
"key": "testKey",
"value": "testValue",
"enabled": true
}
]
}
]
下一步
了解如何从 Postman创建 API 文档,然后将您的文档添加到您的公共工作区。