在 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 文档,然后将您的文档添加到您的公共工作区