Skip to content

REST_CLIENT module

The rest_client module provides a means of interacting with an HTTP server by doing RESTful queries, such as GET and POST.

The following modules must be loaded before this module:

  • No dependencies on other OpenSIPS modules..

The following libraries or applications must be installed before running OpenSIPS with this module loaded:

  • libcurl.

Maximum time allowed to establish a connection with the server.

Default value is “20” seconds.

Setting the connection_timeout parameter
...
modparam("rest_client", "connection_timeout", 300)
...

Maximum number of asynchronous HTTP transfers a single OpenSIPS worker is allowed to run simultanously. As long as this threshold is reached for a worker, all new async transfers it attempts to perform will be done in a blocking manner, with appropriate logging warnings.

Default value is “100”.

Setting the max_async_transfers parameter
...
modparam("rest_client", "max_async_transfers", 300)
...

Maximum time allowed for the libcurl transfer to complete.

Default value is “20” seconds.

Setting the curl_timeout parameter
...
modparam("rest_client", "curl_timeout", 300)
...

Set this to 0 in order to disable the verification of the remote peer’s certificate. Verification is done using a default bundle of CA certificates which come with libcurl.

Default value is “1” (enabled).

Setting the ssl_verifypeer parameter
...
modparam("rest_client", "ssl_verifypeer", 0)
...

Set this to 0 in order to disable the verification that the remote peer actually corresponds to the server listed in the certificate.

Default value is “1” (enabled).

Setting the ssl_verifyhost parameter
...
modparam("rest_client", "ssl_verifyhost", 0)
...

An optional path for CA certificates to be used for host verifications.

Setting the ssl_capath parameter
...
modparam("rest_client", "ssl_capath", "/home/opensips/ca_certificates")
...

rest_get(url, body_pv[, [ctype_pv][, [retcode_pv]]])

Section titled “rest_get(url, body_pv[, [ctype_pv][, [retcode_pv]]])”

Issues an HTTP GET request to the given ‘url’, and returns a representation of the resource.

The body_pv pseudo-var will hold the body of the HTTP response.

The optional ctype_pv pseudo-var will contain the value of the “Content-Type:” header.

The optional retcode_pv pseudo-var is used to retain the HTTP status code of the response message. Since the module is based on libcurl, a 0 value means no HTTP reply arrived at all.

Possible parameter types

  • url - String, pseudo-variable, or a String which includes pseudo-variables. (useful for specifying additional attribute-value fields in the URL)
  • body_pv, ctype_pv, retcode_pv - pseudo-variables

This function can be used from the startup, branch, failure, request and timer routes.

rest_get usage
...
# Example of querying a REST service to get the credit of an account
$var(rc) = rest_get("http://getcredit.org/?account=$fU", "$var(credit)", "$var(ct)", "$var(rcode)");
if ($var(rc) < 0) {
xlog("rest_get() failed with $var(rc), acc=$fU\n");
send_reply("500", "Server Internal Error");
exit;
}
if ($var(rcode) >= 300) {
xlog("L_INFO", "rest_get() rcode=$var(rcode), acc=$fU\n");
send_reply("403", "Forbidden");
exit;
}
...

rest_post(url, send_body_pv, [send_ctype_pv], recv_body_pv[, [recv_ctype_pv][, [retcode_pv]]])

Section titled “rest_post(url, send_body_pv, [send_ctype_pv], recv_body_pv[, [recv_ctype_pv][, [retcode_pv]]])”

Issues an HTTP POST request to the specified url. The request body will be copied from the send_body_pv pseudo-variable. The MIME Content-Type header for the request will be taken from send_ctype_pv (default is “application/x-www-form-urlencoded”)

The mandatory recv_body_pv pseudo-var will hold the body of the HTTP response.

The optional recv_ctype_pv parameter will contain the value of the “Content-Type” header of the response message.

The optional retcode_pv pseudo-var parameter can be given in order to retrieve the HTTP status code of the response message. Since the module based on libcurl, a 0 value means no HTTP reply arrived at all.

Possible parameter types

  • url, send_body_pv, send_type_pv - String, pseudo-variable, or a String which includes pseudo-variables.
  • recv_body_pv, recv_ctype_pv, retcode_pv - pseudo-variables

This function can be used from the startup, branch, failure, request and timer routes.

rest_post usage
...
# Creating a resource using a RESTful service with an HTTP POST request
$var(rc) = rest_post("http://myserver.org/register_user", "$fU", , "$var(body)", "$var(ct)", "$var(rcode)");
if ($var(rc) < 0) {
xlog("rest_post() failed with $var(rc), user=$fU\n");
send_reply("500", "Server Internal Error 1");
exit;
}
if ($var(rcode) >= 300) {
xlog("rest_post() rcode=$var(rcode), user=$fU\n");
send_reply("500", "Server Internal Error 2");
exit;
}
...

rest_get(url, body_pv[, [ctype_pv][, [retcode_pv]]])

Section titled “rest_get(url, body_pv[, [ctype_pv][, [retcode_pv]]])”

Sends a GET HTTP request. This function behaves exactly the same as rest get (in terms of input, output and processing), but in an asynchronous way. Script execution is suspended until the entire content of the HTTP response is available.

async rest_get usage
route {
...
async(rest_get("http://getcredit.org/?account=$fU", "$var(credit)", , "$var(rcode)"), resume);
}
route [resume] {
$var(rc) = $rc;
if ($var(rc) < 0) {
xlog("async rest_get() failed with $var(rc), acc=$fU\n");
send_reply("500", "Server Internal Error");
exit;
}
if ($var(rcode) >= 300) {
xlog("L_INFO", "async rest_get() rcode=$var(rcode), acc=$fU\n");
send_reply("403", "Forbidden");
exit;
}
...
}

rest_post(url, send_body_pv, [send_ctype_pv], recv_body_pv[, [recv_ctype_pv][, [retcode_pv]]])

Section titled “rest_post(url, send_body_pv, [send_ctype_pv], recv_body_pv[, [recv_ctype_pv][, [retcode_pv]]])”

Sends a POST HTTP request. This function behaves exactly the same as rest post (in terms of input, output and processing), but in an asynchronous way. Script execution is suspended until the entire content of the HTTP response is available.

async rest_post usage
route {
...
async(rest_post("http://myserver.org/register_user", "$fU", , "$var(body)", "$var(ct)", "$var(rcode)"), resume);
}
route [resume] {
$var(rc) = $rc;
if ($var(rc) < 0) {
xlog("async rest_post() failed with $var(rc), user=$fU\n");
send_reply("500", "Server Internal Error 1");
exit;
}
if ($var(rcode) >= 300) {
xlog("async rest_post() rcode=$var(rcode), user=$fU\n");
send_reply("500", "Server Internal Error 2");
exit;
}
...
}

Top contributors by DevScore(1), authored commits(2) and lines added/removed(3)

#NameDevScoreCommitsLines++Lines—
1.Liviu Chircu (@liviuchircu)49252094319
2.Stefan Darius (@dariusstefan)8336945
3.Razvan Crainea (@razvancrainea)5377
4.Bogdan-Andrei Iancu (@bogdan-iancu)427122
5.Jarrod Baumann (@jarrodb)312726
6.Andrey Vorobiev (@andrey-vorobiev)2130

(1) DevScore = author_commits + author_lines_added / (project_lines_added / project_commits) + author_lines_deleted / (project_lines_deleted / project_commits)

(2) including any documentation-related commits, excluding merge commits

(3) ignoring whitespace edits, renamed files and auto-generated files

#NameCommit Activity
1.Stefan Darius (@dariusstefan)Jun 2026 - Jul 2026
2.Razvan Crainea (@razvancrainea)Aug 2015 - Jun 2026
3.Liviu Chircu (@liviuchircu)Mar 2013 - Jul 2017
4.Andrey Vorobiev (@andrey-vorobiev)Feb 2017 - Feb 2017
5.Jarrod Baumann (@jarrodb)Apr 2015 - Apr 2015
6.Bogdan-Andrei Iancu (@bogdan-iancu)Oct 2014 - Jan 2015

(1) including any documentation-related commits, excluding merge commits

Last edited by: Razvan Crainea (@razvancrainea), Liviu Chircu (@liviuchircu), Bogdan-Andrei Iancu (@bogdan-iancu).

All documentation files (i.e. .md extension) are licensed under the Creative Common License 4.0