JavaScript API supported by HTTP Client
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:
GET example.org
> {%
console.log(response.status)
%}
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.
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:
|
|
---|---|
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:
< {%
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}}
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:
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"
%}
Thanks for your feedback!