【问题标题】:Need to copy API success response to Clipboard in postman需要在邮递员中将 API 成功响应复制到剪贴板
【发布时间】:2020-11-24 17:08:19
【问题描述】:

我必须将 API 响应复制到剪贴板 (ctrl+c)。我试图在 post man 测试部分通过 document.executeCommand 来做。但是我得到了文档未定义错误。

有没有其他方法可以完成我的要求。

【问题讨论】:

  • 如果您在浏览器上下文中,则不应发生错误 document is not defined。您运行代码的上下文是什么?另外,您是否尝试过使用 Clipboard API ?你也读过stackoverflow.com/questions/400212/… 吗?
  • execCommand 现已过时。
  • 我们不能在邮递员中拥有文档,因为它不是浏览器。这个问题即将找到替代解决方案
  • 我认为邮递员不支持剪贴板 api
  • 不太确定我是否了解您的用例 - 您无法使用响应部分右上角的功能吗?

标签: javascript postman


【解决方案1】:

编辑:我已经完成了。

试试这个:

let template =
`
    <textarea id='copy' style='width:35%'></textarea>
    <script>
        pm.getData(function (error, data)
        {
            var node = document.getElementById('copy');
            node.textContent = data.response;
            var selection = document.getSelection();
            selection.removeAllRanges();
            node.select();
            document.execCommand('copy');
            selection.removeAllRanges();
            document.getElementById('copy').textContent = 'Response copied to clipboard!';
        });
    </script>
`;

pm.visualizer.set(template, {
    response: responseBody
});

上一个答案

您可以以可三次单击(以突出显示)格式将其打印到控制台,从而加快复制速度。我在我的一个收藏中使用了这种技术。

我的示例响应是 JSON 格式,但我想要的部分是嵌套的,所以我使用这个日志语句:

console.info(ζ.name + " Results:\n\r", ζ.results.data[0]);

ζ.results.data[0] 部分是一个方便的、可三次点击的 JSON 字符串。

【讨论】:

  • 如果您想将响应复制到剪贴板作为格式化的 JSON,您显然可以将新答案和我以前的答案结合起来。
猜你喜欢
  • 2017-08-22
  • 2020-07-20
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-07-08
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多