[asterisk-users] Tip/Question about encoding temporary data for storage in Asterisk variable to use in AGI

Jonathan H lardconcepts at gmail.com
Wed May 13 07:23:31 CDT 2020


I wanted to store a JSON object between agi requests for the duration of a
call.

Turns out asterisk does NOT like a stringified JSON object! AGI complains
of "520-Invalid command syntax"

So, I just base64 encode/decode it.

Assuming I don't need to manipulate the JSON object within Asterisk itself,
and I don't want to use a DB or memcache, is this the best/correct method?
WARNING! Asterisk truncates long variables - seems to be about 3000
characters or so(?), so make sure your base64 encoded object is small!

The following is a simple example which just encodes and decodes the agi
variables themselves as they're an object in ts-agi
(I'm using the ts-agi node package here as an example -
https://github.com/sergey12313/ts-agi/ )

    await ctx.setVariable('testvar2',
Buffer.from(JSON.stringify(ctx.variables), 'utf8').toString('base64'))
    const testvar2 = (await ctx.getVariable('testvar2')).value
    const decoded = JSON.parse(Buffer.from(testvar2,
'base64').toString('utf8'))
    console.log('decoded is', decoded)


Hope this helps someone!
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.digium.com/pipermail/asterisk-users/attachments/20200513/191948c4/attachment.html>


More information about the asterisk-users mailing list