[asterisk-users] CURL to post application/json (David P)

Stefan Viljoen viljoens at verishare.co.za
Fri Oct 5 01:26:32 CDT 2018


>We tried to use the CURL fn to POST json, but it's sent as form data and
>there seems no support for changing the Content-Type header. We switched to
>invoking curl in the shell.

Hi David

If you've got if fixed that way, great.

Just thought I'd comment and share the scripts / manner we use this, using CURL, and mention one major gotcha I ran into that took me a few hours to solve related with using CURL in Centos 7 on Asterisk 1.8.32.3, and then switching to 13.22.0.

This is what our dialplan logic looks like to get a UUID from a CURL call made in a BASH file to our back-end application's API.

Dialplan:

same=>n,Set(curlResult=${SHELL(/usr/src/bash/getUUID.sh)})
---

BASH file:

getUUID.sh
---
#!/bin/bash
function jsonval {
    temp=`echo $jsonResult | sed 's/\\\\\//\//g' | sed 's/[{}]//g' | awk -v k="text" '{n=split($0,a,","); for (i=1; i<=n; i++) print a[i]}' | sed 's/\"\:\"/\|/g' | sed 's/[\,]/ /g' | sed 's/\"//g'`
    echo ${temp##*|}
}

baseUrl=http://127.0.0.1/api/getUUID

jsonResult=$(curl --connect-timeout 16 --max-time 32 -s -X GET $baseUrl)

uuid=`jsonval`

echo -n $uuid
---

This returns our UUID (from the Java app) into the dialplan variable curlResult which we then use further on in the dialplan.

The MAJOR gotcha in this above is the

>echo -n $uuid

line...

On Asterisk 1.8.32.3, this worked fine in the BASH script:

echo $uuid

However, on Asterisk 13.22.0, it did NOT work - the dialplan variable curlResult stayed empty on any calls to the BASH script vir SET() in Asterisk.

Turns out that 13.22.0 does NOT like line feed / new line (\n) in ANY strings that come back from BASH.

So I had to change the above line in the BASH file to

echo -n $uuid

so that the line-feed / newline was omitted from being returned to the Asterisk 13 instance.

Just some two cents worth.

Regards

Stefan




More information about the asterisk-users mailing list