You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Hoppscotch v2024.3.0预请求脚本中如何获取/设置新请求变量?

在Hoppscotch v2024.3.0预请求脚本中操作请求变量的正确方式

Hoppscotch v2024.3.0里的请求变量和环境变量是两个独立的概念,你之前用pw.env.get()的方式不对,请求变量需要通过pw.variables对象来操作:

  • 设置请求变量:
    使用pw.variables.set()方法,示例:

    pw.variables.set("my_request_variable", "example_value");
    
  • 获取请求变量:
    使用pw.variables.get()方法,示例:

    const varValue = pw.variables.get("my_request_variable");
    console.log(varValue); // 输出对应变量值
    

注意:pw.env是用来操作全局或环境级变量的,而请求变量属于当前请求专属的临时变量,必须用pw.variables相关方法来读写。

内容的提问来源于stack exchange,提问作者mabi

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.27 02:17:02