Kamailio Bytes – http_client

I’ve touched on the http_client module in Kamailio in the past, and I’ve talked about using Kamailio as an HTTP server.

Today I thought I’d cover a simple use case – running an HTTP get from Kamailio and doing something with the output.

The http_client does what it sounds – Acts as an HTTP client to send HTTP GET and POST requests.

The use cases for this become clear quite quickly, you could use http_client to request credit from an accounting server via it’s API, get the latest rate to a destination from a supplier, pull weather data, etc, etc.

Let’s take a very simple example, we’ll load http_client by adding a loadmodule line:

...
loadmodule "http_client.so"
...

Next I’ve put together a very simple request_route block that requests a HTTP file from a web server and sends the response to a SIP client:

####### Routing Logic ########


/* Main SIP request routing logic
 * - processing of any incoming SIP request starts with this route
 * - note: this is the same as route { ... } */
request_route {

        xlog("Got request");
        http_client_query("https://nickvsnetworking.com/utils/curl.html", "", "$var(result)");
        xlog("Result is $var(result)");
        sl_reply("200", "Result from HTTP server was  $var(result)");
}

Using the http_client_query() function we’re able to query a HTTP server,

We’ll query the URL https://nickvsnetworking.com/utils/curl.html and store the output to a variable called result.

If you visit the URL you’ll just get a message that says “Hello there“, that’s what Kamailio will get when it runs the http_client function.

Next we print the result using an xlog() function so we can see the result in syslog,

Finally we send a stateless reply with the result code 200 and the body set to the result we got back from the server.

We can make this a bit more advanced, using HTTP Post we can send user variables and get back responses.

The http_client module is based on the ubiquitous cURL tool, that many users will already be familiar with.

You can find a full copy of my running code on GitHub.

One thought on “Kamailio Bytes – http_client

  1. Hi Nick, at first I am a big fan of your page, it ‘s great. I have a questin. I have API from which I am getting data a saving them to $var(result). Data are in JSON format. When I use for parsing “s.substr” everything works fine, but now I want to use “json.parse” (json module) transformation and it does not work. In module doocumentation there is this “$var(Custom-Data) = $(rb{json.parse,Custom-Data});” whre $rb is PV from which I am getting data from Custom-Data field. When I use this procedure, it gives me error when starting kamailio, so I try to rewrite to this: “$var(test) = $(var(result{json.parse,Custom-Data}));”. This setup does not return error when starting kamailio, but parsed data is not correct. Don’t know where the problem might be?

Leave a Reply

Your email address will not be published. Required fields are marked *