Apifox 帮助文档
帮助文档常见问题Apifox 官网私有化部署
开发者中心
  • 开放 API
  • 更新日志
  • Road Map
  • Apifox Markdown
下载
  • 下载 Apifox
  • 下载 IDEA 插件
  • 下载浏览器扩展
  • Apifox Web 版
帮助文档常见问题Apifox 官网私有化部署
开发者中心
  • 开放 API
  • 更新日志
  • Road Map
  • Apifox Markdown
下载
  • 下载 Apifox
  • 下载 IDEA 插件
  • 下载浏览器扩展
  • Apifox Web 版
  1. 配置
  • 帮助中心
  • 更新日志
  • 入门
    • 产品介绍
    • 联系我们
    • 私有化部署
  • 开始使用
    • 下载 Apifox
    • 注册与登录
    • 页面布局
    • 基本概念
    • 快速上手
      • 概述
      • 新建接口
      • 发送接口请求
      • 快捷请求
      • 添加断言
      • 新建测试场景
      • 分享 API 文档
      • 了解更多
    • 基础知识
      • 接口基本信息
        • 请求 URL 与方法
        • 请求参数与请求体
        • 请求头
        • 请求参数编码解码
        • HTTP/2
      • 认证与授权
        • 概述
        • 支持的授权类型
        • Digest Auth
        • OAuth 1.0
        • OAuth 2.0
        • Hawk Authentication
        • Kerberos
        • NTLM
        • Akamai EdgeGrid
        • CA 和客户端证书
      • 响应与 Cookie
        • 概述
        • API 响应
        • 创建和发送 Cookie
        • 实际请求
        • 提取响应示例
      • 请求代理
        • 网页端中的请求代理
        • 分享文档中的请求代理
        • 客户端中的请求代理
      • API Hub
        • API Hub
    • 导入导出数据
      • 概述
      • 手动导入
      • 定时导入
      • 导入设置
      • 导出数据
      • 其它方式导入
        • 导入 OpenAPI/Swagger
        • 导入 Postman
        • 导入 Apipost
        • 导入 Eolink
        • 导入 cURL
        • 导入 Markdown
        • 导入 Insomnia
        • 导入 apiDoc
        • 导入 .har 文件
        • 导入 knife4j
        • 导入 NEI
        • 导入小幺鸡(docway)
        • 导入 Apizza
        • 导入 WSDL
  • 设计 API
    • 概述
    • 新建 API 项目
    • 接口基础知识
    • 请求体多示例配置
    • 响应组件
    • 常用字段
    • 全局参数
    • 历史记录
    • 批量管理
    • 数据模型
      • 概述
      • 新建数据模型
      • 通过 JSON 等生成
      • 高级数据类型
      • 构建数据模型
    • 鉴权组件
      • 概述
      • 创建鉴权组件
      • 使用鉴权组件
      • 在线文档中的鉴权组件
    • 高级功能
      • 接口字段
      • 接口状态
      • 关联测试场景
      • 参数列表外观
      • 接口唯一标识
  • 开发和调试 API
    • 概述
    • 生成请求
    • 发送请求
    • 请求历史
    • 接口用例
    • 动态值
    • 校验响应
    • 文档模式/调试模式
    • 生成代码
    • 环境和变量
      • 概述
      • 全局/环境/临时变量
      • 环境与服务
      • Vault Secrets(密钥库)
        • 功能简介
    • 前后置操作&脚本
      • 概述
      • 断言
      • 提取变量
      • 等待时间
      • 数据库操作
        • 概述
        • MongoDB
        • Redis
        • Oracle
      • 使用脚本
        • 概述
        • 前置脚本
        • 后置脚本
        • 公共脚本
        • pm 脚本 API
        • 使用 JS 类库
        • 响应数据可视化
        • 调用外部程序
      • 脚本示例
        • 断言示例
        • 脚本使用变量
        • 脚本读取/修改接口请求信息
      • 常见问题
        • 如何获取动态参数的真实值并加密?
        • 脚本运行后,提取的数字(bigint)精度丢失应该如何处理?
    • API 调试
      • GraphQL 调试
      • WebSocket 调试
      • Socket.IO 调试
      • SSE 调试
      • SOAP/WebService
      • gRPC 调试
      • 使用请求代理调试
      • Dubbo 调试
        • 新建 Dubbo 接口
        • 调试 Dubbo 接口
        • Dubbo 接口文档
      • TCP(Socket)
        • Socket 接口功能简介
        • 报文数据处理器
  • Mock 数据
    • 概述
    • 智能 Mock
    • 自定义 Mock
    • Mock 优先级
    • Mock 脚本
    • 云端 Mock
    • 自托管 Runner Mock
  • 自动化测试
    • 概述
    • 编排测试场景
      • 新建测试场景
      • 测试步骤间传递数据
      • 测试流程控制条件
      • 从接口/用例同步数据
      • 跨项目导入接口/用例
      • 导出测试场景数据
    • 运行测试场景
      • 运行测试场景
      • 批量运行测试场景
      • 数据驱动测试
      • 定时任务
      • 管理其它项目接口的运行环境
    • 测试报告
      • 测试报告
    • API 测试
      • 集成测试
      • 性能测试
      • 端到端测试
      • 回归测试
    • Apifox CLI
      • 概述
      • 安装和运行 CLI
      • CLI 命令选项
    • CI/CD
      • 概述
      • 与 Jenkins 集成
      • 与 Gitlab 集成
      • 与 Github Actions 集成
      • 与其它更多 CI/CD 平台集成
  • 发布 API 文档
    • 概述
    • 快捷分享
    • 查看 API 文档
    • 发布文档站
    • 页面布局设置
    • 自定义域名
    • AI 相关特性
    • 高级设置
      • 文档站搜索设置
      • 跨域代理
      • 文档站接入 Google Analytics
      • SEO 设置
      • 文档左侧目录设置
      • 文档可见性设置
      • 在线 URL 链接规范
    • API 版本
      • 创建 API 版本
      • 发布 API 版本
      • 快捷分享 API 版本
      • 功能简介
  • 迭代分支
    • 功能简介
    • 新建迭代分支
    • 在迭代分支中改动 API
    • 在迭代分支中测试 API
    • 合并迭代分支
    • 管理迭代分支
  • 管理中心
    • 入驻清单
      • 了解基本概念
      • 团队入驻
    • 管理团队
      • 团队基本操作
      • 团队成员管理
      • 成员角色与权限设置
      • 团队资源
        • 通用 Runner
        • 请求代理 Agent
        • 团队变量
      • 实时协作
        • 团队协作
    • 管理项目
      • 项目基本操作
      • 项目成员管理
      • 通知设置
        • 功能简介
        • 通知对象
        • 通知事件
      • 项目资源
        • 数据库连接
    • 管理组织
      • 单点登录(SSO)
        • 功能简介
        • 为组织配置单点登录
        • 管理用户账号
        • 将组映射到团队
        • Microsoft Entra ID
      • SCIM 用户管理
        • 功能简介
        • Microsoft Entra ID
      • 组织资源
        • 自托管 Runner
  • IDEA 插件
    • 快速上手
    • 生成接口文档
    • 生成数据模型
    • 配置
      • 全局配置
      • 项目内配置
      • 可配置规则
      • 脚本工具
      • Groovy 本地扩展
    • 进阶配置
      • 注释规范说明
      • 框架支持
    • 常见问题
      • 常见问题
  • 浏览器扩展
    • Chrome
    • Microsoft Edge
  • Apifox MCP Server
    • 概述
    • 通过 MCP 使用 Apifox 项目内的 API 文档
    • 通过 MCP 使用公开发布的 API 文档
    • 通过 MCP 使用 OpenAPI/Swagger文档
  • 最佳实践
    • 概述
    • 接口之间如何传递数据
    • 登录态(Auth)如何处理
    • 接口签名如何处理
    • 如何加密/解密接口数据
    • Jenkins 定时触发任务
    • 如何计算 AI 问答成本
    • 与其他成员共用数据库连接配置
    • 通过 CLI 运行包含云端数据库连接配置的测试场景
    • 通过 Runner 运行包含云端数据库连接配置的测试场景
  • 账号&应用设置
    • 账号设置
    • 语言设置
    • 网络代理
    • 快捷键
    • 数据备份与恢复
    • 更新 Apifox
    • API 访问令牌
  • 身份验证 & Auth 鉴权指南
    • 什么是 API Key
    • 什么是 Bearer Token
    • 什么是 JWT
    • 什么是 Basic Auth
    • 什么是 Digest Auth
    • 什么是 OAuth 1.0
    • 什么是 OAuth 2.0
      • 什么是 OAuth 2.0
      • 授权码授权类型
      • 授权码授权类型,带有 PKCE
      • 隐式授权类型
      • 密码凭证授权类型
      • 客户端凭证授权类型
  • 服务与隐私协议
    • 服务协议
    • 隐私协议
    • 服务等级协议
  • 参考资料
    • API 设计优先理念
    • JSON Schema 介绍
    • JSONPath 介绍
    • XPath 介绍
    • Apifox Markdown 语法
    • CSV 格式规范
    • 正则表达式
    • 安装 Java 环境
    • Runner 运行环境
    • 常见编程语言对应的数据类型
    • Socket 粘包和分包问题
    • 词汇表
    • 目录链接汇总
    • Apifox Swagger 扩展
      • 概述
      • x-apifox-folder
      • x-apifox-status
      • x-apifox-name
      • x-apifox-maintainer
    • Apifox JSON Schema 扩展
      • 概述
      • x-apifox-mock
      • x-apifox-orders
      • x-apifox-enum
    • 动态值表达式
  • 常见问题
  1. 配置

脚本工具

it - 当前规则的应用对象

  • it 表示当前规则的应用对象(少数规则没有应用对象)
  • 应用对象只提供方法即it.method()✅,不提供属性it.property❎

对于 class(类)、method(方法/API)、field(字段/属性)、param(参数)都支持如下方法

方法返回值描述示例
name()string名称it.name()
hasAnn("annotation_name")boolean是否有指定注解it.hasAnn("org.springframework.web.bind.annotation.RequestBody")
ann("annotation_name")string获取指定注解值(默认 value)it.ann("org.springframework.web.bind.annotation.RequestBody")
ann("annotation_name","attr")string获取指定注解中的指定参数值it.ann("org.springframework.web.bind.annotation.RequestMapping","path")
annMap("annotation_name")map获取指定注解所有的参数it.annMap("org.springframework.web.bind.annotation.RequestMapping")
annMaps("annotation_name")map[]获取指定可重复注解所有的参数it.annMaps("javax.validation.constraints.Max")
annValue("annotation_name")object获取指定注解值(默认 value),与 ann 不同,会保持原有类型,不强制转换为 stringit.annValue("org.springframework.web.bind.annotation.RequestBody")
annValue("annotation_name","attr")object获取指定注解中的指定参数值,与 ann 不同,会保持原有类型,不强制转换为 stringit.annValue("org.springframework.web.bind.annotation.RequestMapping","path")
doc()string获取注释值it.doc()
doc("tag")string获取注释 tag 值(不需要加@)it.doc("return")
docs("tag")array获取多个注释 tag 值(不需要加@)it.docs("see")
doc("tag","subTag")string获取二级注释 tag 值it.doc("param","a")
hasDoc("tag")string是否有指定注释 tagit.hasDoc("ignore")
hasModifier("modifier")string是否有指定修饰it.hasModifier("public")
modifiers()string获取所有修饰it.modifiers()
sourceCode()string获取当前对象源码it.sourceCode()
defineCode()string获取当前对象纯定义代码it.defineCode()
contextType()string当前上下文类型 class/field/method/paramit.contextType()

不同的应用对象提供额外的方法

  • class(类)
方法返回值描述示例
methods()method[]类中的所有方法it.methods()
methodCnt()int类中的方法数it.methodCnt()
field()field[]类中的所有字段it.field()
fieldCnt()int类中的字段数it.fieldCnt()
isExtend(superClass)boolean是否继承某个类it.isExtend("some.class.qualifiedName")
isMap()boolean是否为 Mapit.isMap()
isCollection()boolean是否为集合it.isCollection()
isArray()boolean是否为数组it.isArray()
toJson(readGetter)string获取当前类型的 json 字符串it.toJson(true)
toJson5(readGetter)string获取当前类型的json5字符串it.toJson5(true)
  • method(方法/API)
方法返回值描述示例
returnType()class方法返回类型it.returnType()
isVarArgs()boolean是否有不定参it.isVarArgs()
args()param[]方法的所有参数it.args()
argTypes()class[]方法的所有参数类型it.argTypes()
argCnt()int方法参数个数it.argCnt()
containingClass()class方法所属类it.containingClass()
returnJson(needInfer, readGetter)class当前方法返回对象的 json 字符串it.returnJson(true,true)
  • field(字段/属性)
方法返回值描述示例
type()class字段类型it.type()
containingClass()class字段所属类it.containingClass()
jsonName()string字段 json 名it.jsonName()
jsonType()class字段 json 类型(被转换过的)it.jsonType()
  • param(参数)
方法返回值描述示例
type()class参数类型it.type()
isVarArgs()boolean是否为不定参it.isVarArgs()

tool - 主工具类


主要方法

方法返回值描述示例
debug(obj)voiddebug 对象,获取对象所有可用方法tool.debug(tool)
isNullOrEmpty(obj)Boolean判断对象为 null 或空,支持 null、空字符串、空数组、空集合、空 Maptool.isNullOrEmpty(obj)
notNullOrEmpty(obj)Boolean等价于!isNullOrEmptytool.notNullOrEmpty(obj)
asArray(obj)Array将对象转换为数组tool.asArray(obj)
asList(obj)List将对象转换为集合tool.asList(obj)
intersect(any, other)Array取两个对象的交集tool.intersect(any, other)
anyIntersect(any, other)Boolean判断两个对象是否有交集tool.anyIntersect(any, other)
equalOrIntersect(any, other)Boolean判断两个对象是否相同或有交集,与anyIntersect不同的是, 对于两个空对象, 将返回truetool.equalOrIntersect(any, other)
newSet(items...)Set创建 Set 对象tool.newSet("ele1","ele2")
newList(items...)List创建 List 对象tool.newList("ele1","ele2")
newMap()Map创建 Map 对象tool.newMap()
parseJson(json)Object解析 json 字符串tool.parseJson("{"key":"value"}")
toJson(obj)Object对象转 json 字符串groovy:tool.toJson([key:"value"])
js:tool.toJson({key:"value"})
prettyJson(obj)Object对象转 json 字符串(格式化)groovy:tool.prettyJson([key:"value"])
js:tool.prettyJson({key:"value"})
headLine(str)string获取字符串第一行tool.headLine("multi\nline")
capitalize(str)string字符串首字母大写tool.capitalize("words")
uncapitalize(str)string字符串首字母小写tool.uncapitalize("Words")
swapCase(str)string字符串每个字符大小写转换tool.swapCase("WoRdS")
upperCase(str)string字符串全部大写tool.upperCase("words")
lowerCase(str)string字符串全部小写tool.lowerCase("words")
reverse(str)string字符串反转tool.reverse("sdrow")
repeat(str,repeat)string重复指定字符串tool.repeat("x",10)
isNumeric(str)string判断字符串是纯数字tool.isNumeric("123")
toCamelCase(str)string字符串转驼峰式tool.toCamelCase("some words",true)
now()string获取当前时间 格式为:yyyy-MM-dd HH:mm:sstool.now()
now(pattern)string获取当前时间并指定格式tool.now("yyyy-MM-dd")
today()string获取当前时间 格式为:yyyy-MM-ddtool.today()
format()string格视化时间戳tool.format(1000000000000,"yyyy-MM-dd HH:mm:ss")

其他一些方法


isAlpha

Desc:

Checks if the CharSequence contains only Unicode letters.

{@code null} will return {@code false}. An empty CharSequence (length()=0) will return {@code false}.

tool.isAlpha(null)   = false
tool.isAlpha("")     = false
tool.isAlpha("  ")   = false
tool.isAlpha("abc")  = true
tool.isAlpha("ab2c") = false
tool.isAlpha("ab-c") = false

Params:

nametypedesc
strstring

Return:

nametypedesc
object{@code true} if only contains letters, and is non-null

substringBefore

Desc:

Gets the substring before the first occurrence of a separator. The separator is not returned.

A {@code null} string input will return {@code null}. An empty ("") string input will return the empty string. A {@code null} separator will return the input string.

If nothing is found, the string input is returned.

tool.substringBefore(null, *)      = null
tool.substringBefore("", *)        = ""
tool.substringBefore("abc", "a")   = ""
tool.substringBefore("abcba", "b") = "a"
tool.substringBefore("abc", "c")   = "ab"
tool.substringBefore("abc", "d")   = "abc"
tool.substringBefore("abc", "")    = ""
tool.substringBefore("abc", null)  = "abc"

Params:

nametypedesc
strstring
separatorstring

Return:

nametypedesc
stringthe substring before the first occurrence of the separator,
{@code null} if null String input

substringAfter

Desc:

Gets the substring after the first occurrence of a separator. The separator is not returned.

A {@code null} string input will return {@code null}. An empty ("") string input will return the empty string. A {@code null} separator will return the empty string if the input string is not {@code null}.

If nothing is found, the empty string is returned.

tool.substringAfter(null, *)      = null
tool.substringAfter("", *)        = ""
tool.substringAfter(*, null)      = ""
tool.substringAfter("abc", "a")   = "bc"
tool.substringAfter("abcba", "b") = "cba"
tool.substringAfter("abc", "c")   = ""
tool.substringAfter("abc", "d")   = ""
tool.substringAfter("abc", "")    = "abc"

Params:

nametypedesc
strstring
separatorstring

Return:

nametypedesc
stringthe substring after the first occurrence of the separator,
{@code null} if null String input

substringBeforeLast

Desc:

Gets the substring before the last occurrence of a separator. The separator is not returned.

A {@code null} string input will return {@code null}. An empty ("") string input will return the empty string. An empty or {@code null} separator will return the input string.

If nothing is found, the string input is returned.

tool.substringBeforeLast(null, *)      = null
tool.substringBeforeLast("", *)        = ""
tool.substringBeforeLast("abcba", "b") = "abc"
tool.substringBeforeLast("abc", "c")   = "ab"
tool.substringBeforeLast("a", "a")     = ""
tool.substringBeforeLast("a", "z")     = "a"
tool.substringBeforeLast("a", null)    = "a"
tool.substringBeforeLast("a", "")      = "a"

Params:

nametypedesc
strstring
separatorstring

Return:

nametypedesc
stringthe substring before the last occurrence of the separator,
{@code null} if null String input

substringAfterLast

Desc:

Gets the substring after the last occurrence of a separator. The separator is not returned.

A {@code null} string input will return {@code null}. An empty ("") string input will return the empty string. An empty or {@code null} separator will return the empty string if the input string is not {@code null}.

If nothing is found, the empty string is returned.

tool.substringAfterLast(null, *)      = null
tool.substringAfterLast("", *)        = ""
tool.substringAfterLast(*, "")        = ""
tool.substringAfterLast(*, null)      = ""
tool.substringAfterLast("abc", "a")   = "bc"
tool.substringAfterLast("abcba", "b") = "a"
tool.substringAfterLast("abc", "c")   = ""
tool.substringAfterLast("a", "a")     = ""
tool.substringAfterLast("a", "z")     = ""

Params:

nametypedesc
strstring
separatorstring

Return:

nametypedesc
stringthe substring after the last occurrence of the separator,
{@code null} if null String input

substringBetween

Desc:

Gets the String that is nested in between two instances of the same String.

A {@code null} input String returns {@code null}. A {@code null} tag returns {@code null}.

tool.substringBetween(null, *)            = null
tool.substringBetween("", "")             = ""
tool.substringBetween("", "tag")          = null
tool.substringBetween("tagabctag", null)  = null
tool.substringBetween("tagabctag", "")    = ""
tool.substringBetween("tagabctag", "tag") = "abc"

Params:

nametypedesc
strstring
tagstring

Return:

nametypedesc
stringthe substring, {@code null} if no match

substringBetween

Desc:

Gets the String that is nested in between two Strings. Only the first match is returned.

A {@code null} input String returns {@code null}. A {@code null} open/close returns {@code null} (no match). An empty ("") open and close returns an empty string.

tool.substringBetween("wxbyz", "", "") = "b"
tool.substringBetween(null, *, *)          = null
tool.substringBetween(*, null, *)          = null
tool.substringBetween(*, *, null)          = null
tool.substringBetween("", "", "")          = ""
tool.substringBetween("", "", "]")         = null
tool.substringBetween("", "", "")        = null
tool.substringBetween("yabcz", "", "")     = ""
tool.substringBetween("yabcz", "y", "z")   = "abc"
tool.substringBetween("yabczyabcz", "y", "z")   = "abc"

Params:

nametypedesc
strstring
openstring
closestring

Return:

nametypedesc
stringthe substring, {@code null} if no match

substringsBetween

Desc:

Searches a String for substrings delimited by a start and end tag, returning all matching substrings in an array.

A {@code null} input String returns {@code null}. A {@code null} open/close returns {@code null} (no match). An empty ("") open/close returns {@code null} (no match).

tool.substringsBetween("abc", "", "") = "a","b","c"
tool.substringsBetween(null, *, *)            = null
tool.substringsBetween(*, null, *)            = null
tool.substringsBetween(*, *, null)            = null
tool.substringsBetween("", "", "")          =

Params:

nametypedesc
strstring
openstring
closestring

Return:

nametypedesc
arraya String Array of substrings, or {@code null} if no match
string

split

Desc:

Splits the provided text into an array, using whitespace as the separator. Whitespace is defined by Character#isWhitespace(char).

The separator is not included in the returned String array. Adjacent separators are treated as one separator. For more control over the split use the StrTokenizer class.

A {@code null} input String returns {@code null}.

tool.split(null)       = null
tool.split("")         =
tool.split("abc def")  = "abc", "def"
tool.split("abc  def") = "abc", "def"
tool.split(" abc ")    = "abc"

Params:

nametypedesc
strstring

Return:

nametypedesc
arrayan array of parsed Strings, {@code null} if null String input
string

split

Desc:

Splits the provided text into an array, separators specified. This is an alternative to using StringTokenizer.

The separator is not included in the returned String array. Adjacent separators are treated as one separator. For more control over the split use the StrTokenizer class.

A {@code null} input String returns {@code null}. A {@code null} separatorChars splits on whitespace.

tool.split(null, *)         = null
tool.split("", *)           =
tool.split("abc def", null) = "abc", "def"
tool.split("abc def", " ")  = "abc", "def"
tool.split("abc  def", " ") = "abc", "def"
tool.split("ab:cd:ef", ":") = "ab", "cd", "ef"

Params:

nametypedesc
strstring
separatorCharsstring

Return:

nametypedesc
arrayan array of parsed Strings, {@code null} if null String input
string

regex - 正则工具


getGroup0

Desc:

return the group 0 value($0) if matched otherwise null if not matched

Params:

nametypedesc
regexstring
contentstring

Return:

nametypedesc
string

getGroup1

Desc:

return the group 1 value($1) if matched otherwise null if not matched

Params:

nametypedesc
regexstring
contentstring

Return:

nametypedesc
string

get

Desc:

return the special group value if matched otherwise null if not matched

Params:

nametypedesc
regexstring
contentstring
groupIndexinteger

Return:

nametypedesc
string

getAllGroups

Desc:

return all group value as List if matched otherwise null if not matched

Params:

nametypedesc
patternstring
contentstring

Return:

nametypedesc
array
string

Params:

nametypedesc
regexstring
contentstring
templatestring

Return:

nametypedesc
string

delFirst

Desc:

Remove the first subString of the input String that matches the pattern with the given replacement string.

Params:

nametypedesc
patternstring
contentstring

Return:

nametypedesc
string

delAll

Desc:

Remove the first subString of the input String that matches the pattern

Params:

nametypedesc
regexstring
contentstring

Return:

nametypedesc
string

delBefore

Params:

nametypedesc
regexstring
contentstring

Return:

nametypedesc
string

findAllGroup0

Params:

nametypedesc
regexstring
contentstring

Return:

nametypedesc
array
string

findAllGroup1

Params:

nametypedesc
regexstring
contentstring

Return:

nametypedesc
array
string

findAll

Params:

nametypedesc
regexstring
contentstring
groupinteger

Return:

nametypedesc
array
string

findAll

Params:

nametypedesc
patternobject
- patternstringThe original regular-expression pattern string.
- flagsintegerThe original pattern flags.
contentstring
groupinteger

Return:

nametypedesc
array
string

count

Params:

nametypedesc
regexstring
contentstring

Return:

nametypedesc
integer

contains

Params:

nametypedesc
regexstring
contentstring

Return:

nametypedesc
object

isMatch

Params:

nametypedesc
regexstring
contentstring

Return:

nametypedesc
object

replaceAll

Params:

nametypedesc
contentstring
regexstring
replacementTemplatestring

Return:

nametypedesc
string

escape

Desc:

escape for Regex keywords

Params:

nametypedesc
contentstring

Return:

nametypedesc
string

logger - 日志工具类

可以打印日志到控制台, 受日志级别设置控制, 部分级别的日志会被忽略不显示


方法返回值描述示例
log(str)void打印日志logger.log("some log")
trace(str)void打印日志(日志级别 trace)logger.trace("some log")
debug(str)void打印日志(日志级别 debug)logger.debug("some log")
info(str)void打印日志(日志级别 info)logger.info("some log")
warn(str)void打印日志(日志级别 warn)logger.warn("some log")
error(str)void打印日志(日志级别 error)logger.error("some log")

helper - 特殊帮助类

主要方法

方法返回值描述示例
findClass(str)class获取类, 类似 Class.forName()helper.findClass("java.lang.String")
resolveLink(str)class解析字符串中的linkhelper.resolveLink("{@link java.lang.String}")
resolveLinks(str)array<class/method/field>解析字符串中的所有linkhelper.resolveLink("{@link java.lang.String},{@link java.lang.Long}")

httpClient - HTTP 相关帮助类

主要方法

方法返回值描述示例
cookieStore()cookieStore获取 cookie 管理器httpClient.cookieStore()
request()request创建 requesthttpClient.request()
delete()request创建request,并设置method为DELETEhttpClient.delete()
delete(str)request创建request,并设置method为DELETE、设置urlhttpClient.delete("http://www.apifox.com/")
get()request创建request,并设置method为GEThttpClient.get()
get(str)request创建request,并设置method为GET、设置urlhttpClient.get("http://www.apifox.com/")
options()request创建request,并设置method为OPTIONShttpClient.options()
options(str)request创建request,并设置method为OPTIONS、设置urlhttpClient.options("http://www.apifox.com/")
post()request创建request,并设置method为POSThttpClient.post()
post(str)request创建request,并设置method为POST、设置urlhttpClient.post("http://www.apifox.com/")
put()request创建request,并设置method为PUThttpClient.put()
put(str)request创建request,并设置method为PUT、设置urlhttpClient.put("http://www.apifox.com/")
trace()request创建request,并设置method为TRACEhttpClient.trace()
trace(str)request创建request,并设置method为TRACE、设置urlhttpClient.trace("http://www.apifox.com/")

request

  • 可以由httpClient创建

主要方法有

方法返回值描述示例
call()response发起http请求request.call()
method()string获取当前请求的methodrequest.method()
method(string)request设置当前请求的methodrequest.method("POST")
url()string获取当前请求的urlrequest.url()
url(string)request设置当前请求的urlrequest.url("http://www.apifox.com/")
body()object获取当前请求体request.body()
body(object)request设置当前请求体request.body({"name":"admin"})
contentType()string获取当前请求的Content-typerequest.contentType()
contentType(string)string设置当前请求的Content-typerequest.contentType("application/json")
querys()request获取所有的 query 参数request.querys()
query(string, string)request设置 query(请求时将被追加在 url 后)request.query("name","Tom")
params()array<param>获取所有参数request.params()
params(string)array<param>获取指定参数名的所有参数request.params("name")
containsParam(string)bool判断当前请求是否有指定请求参数request.containsParam("name")
fileParam(string, string)request设置一个文件参数request.fileParam("file","/xxx/xxx/xxxx.xxx")
firstParam(string)param获取第一个请求参数(注意返回值是 param,包含参数名/参数值/参数类型[text/file])request.firstParam("name")
firstParamValue(string)param获取第一个请求参数值request.firstParamValue("name")
lastParam(string)param获取最后一个请求参数(注意返回值是 param,包含参数名/参数值/参数类型[text/file])request.lastParam("name")
lastParamValue(string)param获取最后一个请求参数值request.lastParamValue("name")
param(string, string)request设置paramrequest.param("name","Tom")
paramValues(string)array<string>获取指定参数所有值request.paramValues("name")
header(string, string)request设置headerrequest.header("Content-type","application/json")
headers()array<header>获取所有的headerrequest.headers()
headers(string)array<string>获取指定header所有值request.headers("Content-type")
containsHeader(string)bool判断当前请求是否有指定headerrequest.containsHeader("token")
firstHeader(string)string获取第一个 header 参数request.firstHeader("Content-type")
lastHeader(string)string获取最后一个 header 参数request.lastHeader("Content-type")
setHeader(string, string)request移除当前header名称的所有值,并设置headerrequest.setHeader("Content-type","application/json")
removeHeader(string, string)request移除指定header名称的指定值request.removeHeader("Content-type","application/json")
removeHeaders(string)request移除当前header名称的所有值request.removeHeaders("Content-type")

response

  • 可以由request.call()得到

主要方法有

方法返回值描述示例
discard()void丢弃这次请求的结果, 重新发起此请求response.discard()
code()int响应 coderesponse.code()
string()string响应内容response.string()
contentType()string响应的 contentTyperesponse.contentType()
headers()array<header>获取所有的headerresponse.headers()
headers(string)array<string>获取指定header所有值response.headers("Content-type")
containsHeader(string)bool判断当前响应是否有指定headerresponse.containsHeader("token")
firstHeader(string)string获取第一个 header 参数response.firstHeader("Content-type")
lastHeader(string)string获取第一个 header 参数response.lastHeader("Content-type")
request()request获取得到此响应的requestresponse.request()

cookieStore

  • 可以由httpClient.cookieStore()获得

主要方法有

方法返回值描述示例
addCookie(cookie)void添加一个 cookiehttpClient.cookieStore().addCookie(cookie)
addCookies(cookies)void添加多个 cookiehttpClient.cookieStore().addCookies(cookies)
clear()void清除 cookiehttpClient.cookieStore().clear()
cookies()array<cookie>获得当前的 cookieshttpClient.cookieStore().cookies()
newCookie()cookie创建一个cookie,注意:并不会自动添加到cookieStore中, 需要手动调用addCookie加入httpClient.cookieStore().newCookie()

localStorage 本地存储

  • 未指定分组时所有操作都在默认分组
  • localStorage默认是存储在用户根目录下,可以跨项目读写,如果需要仅在当前项目读写,所有的操作都需要指定一个 group.
  • 注意:set(key,value)中的value与get(key)得到的value并非同一对象.
def obj = ["a":"b"]
localStorage.set("key",obj)
localStorage.get("key")["a"]=c //这里对 get("key")得到的对象进行赋值不会影响到原始的 obj
obj["a"] == "b" //obj 还是["a":"b"]

主要方法

方法返回值描述示例
set(key,value)void设置一个键值localStorage.set("key","value")
set(group,key,value)void设置一个键值(指定分组)localStorage.set("groupA","key","value")
get(key)object获取一个设置的键值localStorage.get("key")
get(group,key)object获取一个设置的键值(指定分组)localStorage.get("groupA","key")
remove(key)void删除一个设置的键值localStorage.remove("key")
remove(group,key)void删除一个设置的键值(指定分组)localStorage.remove("groupA","key")
clear()void移除所有设置的键值(仅默认分组)localStorage.clear()
clear(group)void移除所有设置的键值(指定分组)localStorage.clear("groupA")
keys()array<object>获取所有设置的键值(仅默认分组)localStorage.keys()
keys(group)array<object>获取所有设置的键值(指定分组)localStorage.keys("groupA")

config 配置读取工具

  • 读取当前配置

主要方法

方法返回值描述示例
get(key)string获取配置中的一个值config.get("key")
getValues(key)array<string>获取配置中的多个值config.getValues("key")
resolveProperty(str)string解析字符串中的占位符config.resolveProperty("${key}")

api - 当前导出的 API

  • 应用对象只提供方法即api.method()✅, 不提供属性api.property❎

主要方法

方法返回值描述示例
name()stringapi 名称api.name()
setName("name")void设置 api 名称api.setName("xxx")
desc()stringapi 描述api.desc()
setDesc("desc")void设置 api 描述api.setDesc("这是一个...")
appendDesc("desc")void追加 api 描述api.appendDesc("这是一个...")
method()stringapi 的 Http Methodapi.method()
setMethod("method")void设置 api 的 Http Methodapi.setMethod("POST")
setMethodIfMissed("method")void如果 api 的 Http Method 不存在则设置为指定值api.setMethodIfMissed("POST")
path()stringapi 路径, 如果此 api 有多个路径, 返回第一个api.path()
paths()string[]api 路径数组api.paths()
setPath("path")void设置 api 路径api.setPath("/a/b/c")
setPaths(["path"*])void设置 api 路径api.setPaths(["/a/b/c"."/a/b/d"])
bodyAttr()stringapi body 的描述api.bodyAttr()
setBody(object)void设置 api bodyapi.setBody(["x":"y"])
setBodyClass("class")void通过类型名设置 api bodyapi.setBodyClass("com.itangcent.XxxxDTO")
setJsonBody(object,"attr")void设置 api bodyapi.setBody(["x":"y"],"描述")
addModelAsParam(object)void设置 api 参数api.addModelAsParam(["x":"y"])
addModelClass("class")void通过类型名设置 api 参数api.addModelClass("com.itangcent.XxxxDTO")
addParam(paramName,defaultVal,desc)void增加参数api.addParam("name","tang","user name")
addParam(paramName,defaultVal,required,desc)void增加参数api.addParam("name","tang",false,"user name")
setParam(paramName,defaultVal,required,desc)void设置参数(如果参数已存在会被覆盖)api.setParam("name","tang",false,"user name")
addFormParam(paramName,defaultVal,desc)void增加表单参数api.addFormParam("name","tang","user name")
addFormParam(paramName,defaultVal,required,desc)void增加表单参数api.addFormParam("name","tang",false,"user name")
setFormParam(paramName,defaultVal,required,desc)void设置表单参数(如果参数已存在会被覆盖)api.setFormParam("name","tang",false,"user name")
addFormFileParam(paramName,required,desc)void增加文件类型的表单参数api.setFormParam("avatar",false,"user's avatar")
setFormFileParam(paramName,required,desc)void增加文件类型的表单参数(如果参数已存在会被覆盖)api.setFormFileParam("avatar",false,"user's avatar")
addPathParam(paramName,desc)void增加路径参数api.addPathParam("id","user id")
addPathParam(paramName,value,desc)void增加路径参数api.addPathParam("id","1","user id")
setPathParam(paramName,value,desc)void设置路径参数(如果参数已存在会被覆盖)api.setPathParam("id","1","user id")
headers()array<Header>获取 api 所有的 Headerapi.headers()
header("name")Header获取 api 中指定名称的 Headerapi.header("Content-Type")
headers("name")array<Header>获取 api 中指定名称的所有 Headerapi.headers("token")
removeHeader("name")Header移除 api 中指定名称的 Headerapi.removeHeader("token")
addHeader("name","value")void增加 Headerapi.addHeader("Content-Type","application/json")
addHeaderIfMissed("name","value")void如果指定名称的 Header 不存在则增加此 Headerapi.addHeaderIfMissed("Content-Type","application/json")
addHeader("name","value",required,"desc")void增加 Headerapi.addHeader("Content-Type","application/json",true,"header which is used to indicate the original media type of the resource")
setHeader("name","value",required,"desc")void设置 Header(如果 Header 已存在会被覆盖)api.setHeader("Content-Type","application/json",true,"header which is used to indicate the original media type of the resource")
以下为response相关的方法---------------
setResponseBody(body)void设置 api 响应的 body
bodyType 默认为 rawapi.setResponseBody(["x":"y"])
setResponseBody("bodyType",body)void设置 api 响应的 body
bodyType:raw/json/xmlapi.setResponseBody("json",["x":"y"])
setResponseBodyClass("bodyClass")void通过类型名设置 api 响应的 body
bodyType 默认为 rawapi.setResponseBody("com.itangcent.XxxxVO")
setResponseBodyClass("bodyType","bodyClass")void通过类型名设置 api 响应的 body
bodyType:raw/json/xmlapi.setResponseBody("json","com.itangcent.XxxxVO")
setResponseCode(code)void设置响应 codeapi.setResponseCode(200)
appendResponseBodyDesc("bodyDesc")void设置响应描述api.appendResponseBodyDesc("user info")
addResponseHeader("name","value")void增加响应 Headerapi.addResponseHeader("Content-Type","application/json")
addResponseHeader("name","value",required,"desc")void增加响应 Headerapi.addResponseHeader("Content-Type","application/json",true,"header which is used to indicate the original media type of the resource")
setResponseHeader("name","value",required,"desc")void设置响应 Headerapi.setResponseHeader("Content-Type","application/json",true,"header which is used to indicate the original media type of the resource")
修改于 2025-01-07 07:07:17
上一页
可配置规则
下一页
Groovy 本地扩展
Built with