http.get
Retrieves data from an HTTP source.
http.get(url, query)
.then(function(response) {
...
});
// or with await:
var response = await http.get(url, query);Input
Variable | Required | Description |
|---|---|---|
| yes | The URL to retrieve. |
| optional | (object) Optional URL query string parameters. Can be appended to the URL or passed as an object. |
Output
This function returns a Promise with the following data:
Variable | Type | Description |
|---|---|---|
| boolean | True if the command completed successfully. |
| string | Error message if the command failed. |
| number | (On success) The HTTP status code. |
| array | (On success) Array containing the HTTP headers. |
| string | (On success) The body of the reply as a string. |
Comments
This function performs an HTTP GET request to the specified URL.
Query parameters can be either:
appended directly to the URL, e.g.
example.com/?param1=1¶m2=2, orpassed as an object, e.g.
{ param1: 1, param2: 2 }
Limitations
The response body length is limited to 4096 bytes.
Examples
console.log("Doing http request...");
http.get("https://mysmartbots.com")
.then(function(response) {
console.log("http result:", response.body);
})
.then(function() {
// Gracefully stop the test script
exit();
});A more advanced example with error handling:
console.log("Doing http request...");
http.get("https://mysmartbots.com")
.then(function(result) {
if (!result.success) {
// On error display the error message and stop the processing
console.log("HTTP error:", result.error);
throw "";
}
console.log("http result:", result.body);
})
.catch(function(err) {
// This block allows cancelling the processing chain with throw ""
if (err != "") { throw err; }
})
.then(function() {
// Gracefully stop the test script
exit();
});