Difference between revisions of "Bot Playground/Built-in Functions/http.post"
From SmartBots Developers Docs
Line 1: | Line 1: | ||
{{DISPLAYTITLE:{{SUBPAGENAME}}}} | {{DISPLAYTITLE:{{SUBPAGENAME}}}} | ||
<onlyinclude>Retrieves data from a HTTP source using the POST method.</onlyinclude> | <onlyinclude>Retrieves data from a HTTP source using the POST method.</onlyinclude> | ||
− | + | ||
+ | <syntaxhighlight lang="javascript"> | ||
+ | http.post(url, query) | ||
+ | .then(function(response) { | ||
+ | ... | ||
+ | }); | ||
+ | </syntaxhighlight> | ||
+ | |||
+ | or with [[Bot_Playground/Async_and_await|await]]: | ||
+ | |||
+ | <syntaxhighlight lang="javascript"> | ||
+ | var response = await http.post(url, query); | ||
+ | </syntaxhighlight> | ||
+ | |||
+ | |||
+ | {{API Command Table}} | ||
+ | {{API Required Vars|login}} | ||
+ | |||
+ | {{API Variable Group|Input}} | ||
+ | {{API Variable|url|yes}} the URL to retrieve | ||
+ | {{API Variable|query|no}} (object) the optional POST parameters, object | ||
+ | |||
+ | |||
+ | {{API Variable Group|Output}} | ||
+ | {{API Return promise}} | ||
+ | {{API Variable|statusCode}} (on success) The HTTP status code | ||
+ | {{API Variable|headers}} (on success) The array with HTTP headers | ||
+ | {{API Variable|body}} (on success) The string body of the reply | ||
+ | {{API Variables Table End}} | ||
+ | |||
+ | == Comments == | ||
+ | |||
+ | This function makes an HTTP POST request to the specified URL. The query parameters may be added to the URL as a '''query''' object ('''{ param1: 1, param2: 2 }'''). | ||
+ | |||
+ | You can use https://ptsv2.com to test your POST requests. | ||
+ | |||
+ | === Limitations === | ||
+ | |||
+ | The length of the response body is limited to 4096 bytes. | ||
+ | |||
+ | === Example === | ||
+ | <syntaxhighlight lang="javascript"> | ||
+ | console.log("Doing http request..."); | ||
+ | |||
+ | var response = await http.post("https://ptsv2.com/t/h6rf5-1631886515/post", { param1: 1, param2: 2}) | ||
+ | console.log("http result:", response.body); | ||
+ | |||
+ | // 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.post("https://ptsv2.com/t/h6rf5-1631886515/post", { param1: 1, param2: 2}) | ||
+ | .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(); | ||
+ | }); | ||
+ | </syntaxhighlight> | ||
+ | |||
+ | {{NavMenu}} | ||
+ | __NOTOC__ |
Revision as of 13:49, 17 September 2021
Retrieves data from a HTTP source using the POST method.
http.post(url, query)
.then(function(response) {
...
});
or with await:
var response = await http.post(url, query);
Reference
This command accepts the following parameters:
Variable | Required | Description
| |
---|---|---|---|
Input: | |||
url | yes | the URL to retrieve | |
query | optional | (object) the optional POST parameters, object
| |
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 POST request to the specified URL. The query parameters may be added to the URL as a query object ({ param1: 1, param2: 2 }).
You can use https://ptsv2.com to test your POST requests.
Limitations
The length of the response body is limited to 4096 bytes.
Example
console.log("Doing http request...");
var response = await http.post("https://ptsv2.com/t/h6rf5-1631886515/post", { param1: 1, param2: 2})
console.log("http result:", response.body);
// Gracefully stop the test script
exit();
A bit more complex example which provides the error handling:
console.log("Doing http request...");
http.post("https://ptsv2.com/t/h6rf5-1631886515/post", { param1: 1, param2: 2})
.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();
});