Welcome to our new website! Some content may have moved. If you can’t find what you need, try our search or get in touch and we’ll be happy to help.

Speak to a website designer — call Call 01793 239239 Our team are here 9am to 5pm weekdays Client Connect

The Digital Insider Blog

Asterisk Dialplan using Curl and Json

Asterisk Dialplan using Curl and Json

In this article we are going to see how we can use cURL to query an external HTTP service and read a response in JSON format and take action on the values returned to control the call flow in our…

It’s very common that we want to use external services from our Asterisk Dialplan, and many times those external services are accessible via HTTP (such as a REST HTTP API).

In this article we are going to see how we can use cURL to query an external HTTP service and read a response in JSON format and take action on the values returned to control the call flow in our dialplan.

Using cURL to query an HTTP API from the Asterisk Dialplan

Asterisk has the CURL dialplan function that can be used to issue HTTP requests from the dialplan.

https://gist.github.com/swchris/fd919067aa281952e0060c6de354733d

Once the request is done we can access the result (the body of the request) in the variable CURL_RESULT, by using the dialplan Set Application to set the variable value.

Using HTTPS to query REST APIs from the dialplan

In case you need to access an HTTPS endpoint with a self signed certificate or other SSL issues that may fail the HTTPS request, you can use the CURLOPT dialplan function to disable the ssl_verifypeer in cURL before doing the request:

https://gist.github.com/swchris/423a070db3ea3911478d5eb37d0cda2c

Using the HTTP request result to fork in the Asterisk Dialplan

By using the GotoIf application of the Asterisk Dialplan one can take action depending on the value returned by the HTTP request:

https://gist.github.com/swchris/8f2b43c8fbe1c061ab7633cb251c928b

The code above will issue a request for the file test.txt, and depending on the exact value will fork the execution to either the label result1 or result2, which is quite useful! Remember that we are checking the exact value (content) of the request, which in this case is the content of the test.txt file.

Using res_json in the Asterisk Dialplan to process a JSON string

It’s quite common for APIs to return a JSON payload, and we can use res_json in our dialplan to parse the contents we get as a result of an HTTP request and act accordingly. This can be achieved by compilling and installing res_json according to the instructions and then from the dialplan:

https://gist.github.com/swchris/c7986064221b27a63d9b1e240e8d8aee

We can test this by having the following contents for test.json:

https://gist.github.com/swchris/b25eca98c9cdd0dd898312e9f6beb407

Conclusion

Without too much effort we are now able to call HTTP APIs that return plain text or JSON strings and process them in our Asterisk dialplans to take decisions. Enjoy!

Keep reading

Related posts

Need a hand with your website?

Get in touch and we'll be happy to help — no tricks, traps or technobabble.