Difference between revisions of "Bot Playground/Built-in Functions/http.get"
From SmartBots Developers Docs
(4 intermediate revisions by the same user not shown) | |||
Line 5: | Line 5: | ||
http.get(url, query) | http.get(url, query) | ||
.then(function(response) { | .then(function(response) { | ||
− | |||
− | |||
− | |||
... | ... | ||
}); | }); | ||
</syntaxhighlight> | </syntaxhighlight> | ||
+ | |||
+ | or with [[Bot_Playground/Async_and_await|await]]: | ||
+ | |||
+ | <syntaxhighlight lang="javascript"> | ||
+ | var response = await http.get(url, query); | ||
+ | </syntaxhighlight> | ||
+ | |||
{{API Command Table}} | {{API Command Table}} | ||
Line 37: | Line 41: | ||
=== Example === | === Example === | ||
<syntaxhighlight lang="javascript"> | <syntaxhighlight lang="javascript"> | ||
− | console.log("Doing http request"); | + | console.log("Doing http request..."); |
http.get("https://mysmartbots.com") | http.get("https://mysmartbots.com") | ||
Line 43: | Line 47: | ||
console.log("http result:", response.body); | console.log("http result:", response.body); | ||
}) | }) | ||
+ | |||
+ | .then(function() { | ||
+ | // Gracefully stop the test script | ||
+ | exit(); | ||
+ | }); | ||
+ | </syntaxhighlight> | ||
+ | |||
+ | A bit more complex example which provides the error handling: | ||
+ | <syntaxhighlight lang="javascript"> | ||
+ | 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) { | .catch(function(err) { | ||
− | + | // This block allows cancelling the processing chain with throw "" | |
+ | if(err != "") { throw err; } | ||
+ | }) | ||
+ | .then(function() { | ||
+ | // Gracefully stop the test script | ||
+ | exit(); | ||
}); | }); | ||
</syntaxhighlight> | </syntaxhighlight> | ||
{{NavMenu}} | {{NavMenu}} | ||
+ | __NOTOC__ |
Latest revision as of 13:46, 17 September 2021
Retrieves data from a HTTP source.
http.get(url, query)
.then(function(response) {
...
});
or with await:
var response = await http.get(url, query);
Reference
This command accepts the following parameters:
Variable | Required | Description
| |
---|---|---|---|
Input: | |||
url | yes | the URL to retrieve | |
query | optional | (object) the optional URL query string params
| |
Output: | |||
Function returns a Promise with the following data: | |||
success | bool | true if command completed successfully | |
error | string | error string if command has failed | |
statusCode | (on success) The HTTP status code | ||
headers | (on success) The array with HTTP headers | ||
body | (on success) The string body of the reply |
Comments
This function makes an HTTP GET request to the specified URL. The query string may be added to the URL (example.com/?param1=1¶m2=2) or passes as a query object ({ param1: 1, param2: 2 }).
Limitations
The length of the body is limited to 4096 bytes.
Example
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 bit more complex example which provides the 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();
});