【问题标题】:Fetch the body of a request on chrome and use it on a postman request在 chrome 上获取请求的正文并将其用于邮递员请求
【发布时间】:2018-04-12 01:57:17
【问题描述】:

我正在尝试获取来自 python 脚本的请求的 JSON 回复:

import webbrowser
webbrowser.open(url, new=0, autoraise=True)

...并将其作为 Postman 请求中的主体发送到不同的 API。

脚本有多次迭代。我可以用它在 Chrome 中打开 50 个标签,但现在我如何将复制粘贴正文过程自动化到 Postman。

【问题讨论】:

    标签: python google-chrome postman


    【解决方案1】:

    没有真正受支持的方式来与来自不同进程的 Postman 应用程序通信以运行请求(参见例如 https://github.com/postmanlabs/postman-app-support/issues/86),它超出了该工具的范围。

    如果我很好地理解了问题的要点;基本上,您想要实现的是连接几个网络请求,在它们之间重用数据。在我看来,您可以采取以下几种方法:

    在你的 python 代码上执行这两个请求:

    您不需要打开浏览器来发送 HTTP 请求的开销;相反,只需使用一个库——比如urllibrequests——来完成这项工作。这是一个例子:

    import req
    # First request is a GET to "url"
    r1 = requests.get(url)
    # POST the contents of r1's reply to a different endpoint:
    r2 = requests.post("http://url.tld/endpoint", data=r1.json())
    

    您可以从那里继续在脚本中查询r2 以了解详细信息或操作数据。

    创建 2 个不同的 Postman 请求并依次运行它们

    同样,您可以在邮递员中创建这两个请求,使用test 选项卡提取响应的主体,将其保存为环境变量,然后在下一个请求中重用它。

    var body = pm.response.json();
    pm.environment.set("body", body);
    

    您可以访问 env.var。使用车把符号 - 即{{body}}

    请注意,如果您有任何额外的原因想要从浏览器发出请求(例如 cookie),您可以从 Network tab on the Chrome devtoolsimport it back to postman 手动将其导出为 cURL 或使用 Postman interceptor 捕获浏览器的活动。这将保留标题/任何其他数据,以便将来能够重用它。

    【讨论】:

      猜你喜欢
      • 2020-07-29
      • 1970-01-01
      • 2017-12-29
      • 2020-03-11
      • 1970-01-01
      • 2017-06-17
      • 1970-01-01
      • 1970-01-01
      • 2018-10-22
      相关资源
      最近更新 更多