GoLand 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 树方法与属性,允许您解析和操作响应正文中收到或使用 DOMParser 创建的 XML 和 HTML 文档。 支持的方法包括:

getElementsByTagName()

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

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

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

getElementById()

通过其 id 检索元素对象。

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() 完全相同,但该函数将在子进程完全关闭之前不会返回。

支持的选项包括:

指定应在哪个目录中执行命令。

指定应在哪个目录中执行命令。

指定应提供给进程的一组环境变量。

指定应提供给进程的一组环境变量。

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

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

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

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

指定用于执行命令的 shell。

指定用于执行命令的 shell。

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

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

不是一个选项,而是一个单独的参数,可传递给异步方法。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日