JavaScript API supported by HTTP Client
console.log
The HTTP Client supports the console.log()
method to print the text value (or multiple values separated by commas) to the output of the response handler or pre-request handler scripts. You can also use client.log for the same purpose. Example:
You can also pass a JavaScript object (for example, console.log(response.body)
), and it will be displayed in the output in JSON format with proper syntax highlighting.
DOM methods and properties
The HTTP Client supports some of the DOM tree methods and properties that allow you to parse and manipulate XML and HTML documents received as part of the response body (or created using DOMParser). Supported methods include:
- getElementsByTagName()
Returns a collection of elements with the given tag name. For example:
GET https://examples.http-client.intellij.net/xml > {% const slide = response.body.getElementsByTagName("slide")[0] console.log(slide) %}- getElementsByName()
Returns a collection of nodes with a given
name
attribute. For example:- getElementById()
Retrieves an Element object by its
id
.- getElementsByClassName()
Returns an array-like object of all child elements with the given class name.
- DOMParser
Parse XML or HTML source code from a string into a DOM Document using the
parseFromString()
method. For example: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)
Creates the HTML element specified by
tagName
.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"); %}
Start typing a method name to get completion for supported methods. Hover over it to get quick documentation. To view all supported DOM methods and properties, expand the sections below:
Supported methods
|
|
---|---|
Supported properties
URLSearchParams
URLSearchParams
is a JavaScript object that makes it easy to work with the query string part of a URL. URLSearchParams
accepts parameters in the following formats:
Query string:
URLSearchParams("key=value&key2=value2");
Key-value pairs representing URL parameters:
URLSearchParams({ key1: "value1", key2: "value2" })
Array of key-value pairs:
URLSearchParams([["key1", "value1"], ["key2", "value2"]])
The HTTP Client supports all known URLSearchParams methods. Example:
Base64 encoding: btoa and atob
The HTTP Client supports the btoa() and atob() methods.
Use
Window.btoa(stringToEncode)
to create a Base64-encoded ASCII string from astringToEncode
string.Use
Window.atob(encodedData)
to decode a string of data which has been encoded using Base64 encoding.
For example:
Execute shell commands
To let you interact with the operating system by running shell commands, the HTTP Client supports the following methods:
- exec(command[, options][, callback])
Spawns a shell and then executes the command within that shell, buffering any generated output.
- execFile(file[, args][, options][, callback])
Similar to
exec()
except that it does not spawn a shell by default.- execSync(command[, options])
Identical to
exec()
with the exception that the method will not return until the child process has fully closed.- execFileSync(file[, args][, options])
identical to
execFile()
with the exception that the method will not return until the child process has fully closed.- spawn(command[, args][, options])
Spawns a shell, then executes the command within that shell, buffering any generated output.
- spawnSync(command[, args][, options])
Identical to
spawn()
with the exception that the function will not return until the child process has fully closed.
The supported options include:
- cwd
Specifies the directory in which the command should be executed.
- env
Specifies a set of environment variables that should be available to the process.
- timeout
Maximum duration (in milliseconds) the process is allowed to run.
- maxBuffer
Sets a maximum limit (in characters) for the size of the output.
- shell
Specifies the shell to execute the command with.
- signal
Specifies the signal to be sent to the process when it needs to be terminated
- callback
Not an option, but a separate parameter, which can be passed to the asynchronous methods. The callback is a function that will be called when the process terminates.
For example: