EMG and custom HTTP requests (outbound) Print
Wednesday, 06 May 2015

HTTP connectors in EMG uses a default format where MGP options are used as key-value pairs in a HTTP POST or GET request.

The format is well defined and we provide a sample HTTP API description.

However, sometimes it is necessary to integrate with service providers that require a different format for their HTTP apis.

In those cases it is fairly simple to put together a perl script which can be configured as a EBE connector in EMG.

Any type of http request (get or post) can be created this way with data formatted as key-value, xml, json etc.

Sample code

Sample script for formatting and sending a custom http request in perl:

#!/opt/perl-5.12.2-emg/bin/perl use LWP; use URI::Escape; my $ua = LWP::UserAgent->new; my $username = 'user'; my $password = 'secret'; my $options; #open(OUT, ">/tmp/ebe.log"); while(<STDIN>) { chomp; my($key, $value) = split(/\t/); $options->{$key} = $value; #print OUT "key=$key value=$value\n"; } #close OUT; my $id = $options->{1} || -1; my $destaddr = $options->{8} || ''; my $msg = $options->{16} || ''; # Hex decode message $msg =~ s/(..)/pack("C", hex($1))/eg;

# Encode message data with recipient and message body tab-separated
my $msgdata = uri_escape("$destaddr\t$msg");
my $url = "http://api.example.com/sms?user=$username&pass=$password&linesep=0&testmode=0&messages=$msgdata";
# HTTP GET request
my $resp = $ua->get( $url );
exit 1 unless $resp->is_success;
exit 0;

Sample server.cfg entry

Corresponding connector configuration in server.cfg:

CONNECTOR ebe-http-custom < TYPE=OUTGOING PROTOCOL=EBE ADDRESS=/home/emg/etc/ebe-http-custom.pl INSTANCES=1 >