V3 Request HTTP API resources
In this article we will see how to request HTTP API resources and use it in Cally Square project.
Method 1: RestAPI block
Using RestAPI block in Cally Square
Fill the RestAPI block as described below:
Assuming the response of the request is:
{ "userId": 1, "id": 1, "title": "delectus aut autem" }
the application will set following variables:
RESULT_STATUS_CODE "200"
RESULT_STATUS_MESSAGE "OK"
RESULT.userId "1"
RESULT.id "1"
RESULT.title "delectus aut autem"
Method 2: External Script
Create script
Below you can see an example of script written in Perl language, but you can use your favourite language.
If you want to run the following example, make sure that:
- perl is installed in your system
- script dependencies are installed in your system
-
yum install perl-CPAN -
cpan install JSON
example.agi
#!/usr/bin/perl -w
use strict;
use warnings;
use JSON qw( decode_json );
my $url = "https://jsonplaceholder.typicode.com/todos/1";
my $curl=`curl -k '$url'`;
# { "userId": 1, "id": 1, "title": "delectus aut autem", "completed": false }
my $decoded = decode_json($curl);
my $title = $decoded-{'title'};
print "SET VARIABLE RESULT_TITLE \"$title\"\\n";
my $userId = $decoded-{'userId'};
print "SET VARIABLE RESULT_USERID \"$userId\"\\n";
print "SET VARIABLE <CHANNEL_VARIABLE> \"<VARIABLE>\"\\n";
The script can be used as reference for your needs and remember to set properly the result of the cURL command:
Use script in Cally Square project
The script must be used as Command in AGI block:
example.agi is using needed permissions (execution)
And the variable set in the example.agi script will be available in Cally Square project.
As alternative of AGI Cally Square block, you can use System block assigning the result of the script as channel variable.
Source: “V3 Request HTTP API resources — XCALLY Motion V3 — XCALLY WIKI”.
The following block shows how the RestAPI block appears in Cally Square within XCALLY Motion V3, including the variable format and an example API response.
This is how the Use script in Cally Square project section looks. The script must be used as a Command in the AGI block.
Comments
0 comments
Article is closed for comments.