WebStorm 2025.2 Help

HTTP Client 支持的 JavaScript API

console.log

HTTP Client 支持使用 console.log() 方法将文本值(或用逗号分隔的多个值)输出到响应处理器或请求前处理器脚本的输出中。 您也可以使用 client.log 来实现相同目的。 例如:

GET example.org > {% console.log(response.status) %}

您也可以传递一个 JavaScript 对象(例如 console.log(response.body) ),并将在输出中以带有语法高亮的 JSON 格式显示。

DOM 方法与属性

HTTP Client 支持部分 DOM 树方法与属性,可用于解析与操作响应正文中接收的 XML 和 HTML 文档(或使用 DOMParser 创建的文档)。 支持的方法包括:

getElementsByTagName()

返回具有指定标签名的元素集合。 例如:

GET https://examples.http-client.intellij.net/xml > {% const slide = response.body.getElementsByTagName("slide")[0] console.log(slide) %}
getElementsByName()

返回具有指定 name 属性的节点集合。 例如:

getElementById()

根据 id 获取对应的 Element 对象。

getElementsByClassName()

返回具有指定类名的所有子元素的类数组对象。

DOMParser

使用 parseFromString() 方法将字符串中的 XML 或 HTML 源代码解析为 DOM 文档。 例如:

GET example.org > {% const xmlStr = '<q id="a"><span class="test" id="b">hey!</span><span id="bar">world</span><foo class="test" id="x-foo"/><bar name="x-foo"/></q>'; const doc = new DOMParser().parseFromString(xmlStr, "application/xml"); console.log(doc.getElementById("a")) console.log(doc.getElementById("b")) console.log(doc.getElementsByClassName("test")) console.log(doc.getElementsByTagName("span")) console.log(doc.getElementsByName("x-foo")) %}
createElement(tagName)

创建由 tagName 指定的 HTML 元素。

GET example.org > {% const xmlStr = '<q id="a"><span class="test" id="b">hey!</span><span id="bar">world</span><foo class="test" id="x-foo"/><bar name="x-foo"/></q>'; const doc = new DOMParser().parseFromString(xmlStr, "application/xml"); const bodyElement = doc.createElement("body"); %}

开始输入方法名称以获取支持方法的补全。 将鼠标悬停在其上可查看快速文档。 要查看所有支持的 DOM 方法和属性,请展开以下部分:

URLSearchParams

URLSearchParams 是一个 JavaScript 对象,可用于轻松操作 URL 的查询字符串部分。 URLSearchParams 可接受以下格式的参数:

  • 查询字符串: URLSearchParams("key=value&key2=value2");

  • 表示 URL 参数的键值对: URLSearchParams({ key1: "value1", key2: "value2" })

  • 键值对数组: URLSearchParams([["key1", "value1"], ["key2", "value2"]])

HTTP Client 支持 所有已知的 URLSearchParams 方法。 例如:

< {% client.global.set('query', 'foo=1&bar=2') const params = new URLSearchParams(client.global.get('query')); const params2 = new URLSearchParams([["planet", "tatooine"], ["year", "2"]]); const params3 = new URLSearchParams({key1: "value1", key2: "value2"}); console.log(params.has("bar")); // outputs true console.log(params.has("param")); // outputs false params.append("foo", 3); console.log(params.getAll("foo")); // outputs ["1","3"] for (let value of params.values()) { console.log(value); // outputs 1 2 3 } for (let key of params2.keys()) { console.log(key); // outputs planet year } client.global.set("query",params.toString()) %} GET example.org/{{query}}

Base64 编码:btoa 与 atob

HTTP Client 支持 btoa()atob() 方法。

  • 使用 Window.btoa(stringToEncode) 可将 stringToEncode 字符串转换为 Base64 编码的 ASCII 字符串。

  • 使用 Window.atob(encodedData) 可解码使用 Base64 编码表示的数据字符串。

例如:

GET https://examples.http-client.intellij.net/ip > {% const encodedData = Window.btoa("Hello, world"); // encode a string const decodedData = Window.atob(encodedData); // decode the string console.log(decodedData); // prints "Hello, world" %}

执行 Shell 命令

为了让您通过运行 shell 命令与操作系统交互,HTTP Client 支持以下方法:

exec(command[, options][, callback])

启动一个 shell,然后在该 shell 中执行命令,并缓冲生成的所有输出。

execFile(file[, args][, options][, callback])

exec() 类似,但默认不会启动 shell。

execSync(command[, options])

exec() 相同,仅有的区别是该方法会在子进程完全关闭之前不会返回。

execFileSync(file[, args][, options])

execFile() 相同,仅有的区别是该方法会在子进程完全关闭之前不会返回。

spawn(command[, args][, options])

启动一个 shell,然后在该 shell 中执行命令,并缓冲生成的所有输出。

spawnSync(command[, args][, options])

spawn() 相同,仅有的区别是该函数会在子进程完全关闭之前不会返回。

支持的选项包括:

cwd

指定应在其中执行命令的目录。

env

指定应对该进程可用的一组环境变量。

timeout

允许进程运行的最长时间(以毫秒为单位)。

maxBuffer

设置输出大小的最大限制(以字符为单位)。

shell

指定用于执行命令的 shell。

signal

指定在需要终止进程时要发送的信号。

callback

不是选项,而是一个单独的参数,可传递给异步方法。 callback 是在进程终止时会调用的函数。

例如:

### List all directories in the specified folder GET https://examples.http-client.intellij.net > {% const result = execSync("ls -l", { cwd: '/Users/John.Doe/Docs/' }) console.log(`result: ${result}`) %} ### Pass an environment variable to the process < {% const result = execSync('bash -c "set"', { env : { name: 'John' }}) console.log(`result: ${result}`) %} GET https://examples.http-client.intellij.net
最后修改日期: 2025年 9月 26日