How to request HTTP API resources

How to request HTTP API resources

Overview

In this article you will learn how to request HTTP API resources to be used 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. You can use your preferred programming 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 is using needed permissions (execution)

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";

The script can be used as reference for your needs and remember to set properly the result of the cURL command:

print "SET VARIABLE <CHANNEL_VARIABLE> \"<VARIABLE>\"\n";

 

Use script in Cally Square project

The script must be used as Command in AGI block. The variable set in the example.agi script will be available in the Cally Square project.

 

As alternative of AGI Cally Square block, you can use System block assigning the result of the script as channel variable.