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, POST and PUT.

Unless specified otherwise by the server through a “Connection: close” indication, the module will keep and reuse the TCP connections it creates as much as possible, regardless if the script writer performs blocking or asynchronous HTTP requests. These connections are not shared among OpenSIPS workers — each worker maintains its own set of connections.

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.

The maximum allowed time for any HTTP(S) transfer to complete. This interval is inclusive of the initial connect time window, hence the value of this parameter must be greater than or equal to connection timeout.

Default value is “20” seconds.

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

The maximum allowed time to establish a connection with the server.

Default value is “20” seconds.

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

Only relevant with async requests. Allows complete control over how quickly we want to detect libcurl’s completed blocking TCP/TLS handshakes, so the async transfers can be put in the background. A lower connect poll interval may speed up all async HTTP transfers, but will also increase CPU usage.

Default value is “20” milliseconds.

Setting the connect_poll_interval parameter
...
modparam("rest_client", "connect_poll_interval", 2)
...

Maximum number of asynchronous HTTP transfers a single OpenSIPS worker is allowed to run simultaneously. 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)
...

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")
...

Include a “Expect: 100-continue” HTTP header field whenever the body size of a POST or PUT request exceeds 1024 bytes. Once enabled, the timeout for waiting for a “100 Continue” reply from the server is 1 second, after which the body upload will begin.

Default value is “false” (disabled).

Setting the enable_expect_100 parameter
...
modparam("rest_client", "enable_expect_100", true)
...

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

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

Perform a blocking HTTP GET on the given url and return 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 of the response.

The optional retcode_pv pseudo-var will retain the status code of the HTTP response. A 0 status code value means no HTTP reply arrived at all.

Parameter Types

  • url - string, pseudo-variable or pseudo-variable format string
  • body_pv, ctype_pv, retcode_pv - pseudo-variables

Return Codes

  • 1 - Success
  • -1 - Connection Refused.
  • -2 - Connection Timeout (the connection timeout was exceeded before a TCP connection could be established)
  • -3 - Transfer Timeout (the curl timeout was exceeded before the last byte was received). The retcode_pv may be set to 200 or 0, depending whether a 200 OK was received or not. If it was, the body_pv will contain partially downloaded data, use at your own risk! (we recommend you only use this data for logging / debugging purposes)
  • -10 - Internal Error (out of memory, unexpected libcurl error, etc.)

This function can be used from any route.

rest_get usage
...
# Example of querying a REST service to get the credit of an account
$var(rc) = rest_get("https://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) != 200) {
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]]])”

Perform a blocking HTTP POST on the given 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 (the 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.

The optional retcode_pv pseudo-var will retain the status code of the HTTP response. A 0 status code value means no HTTP reply arrived at all.

Parameter Types

  • url, send_body_pv, send_type_pv - string, pseudo-variable or pseudo-variable format string
  • body_pv, ctype_pv, retcode_pv - pseudo-variables

Return Codes

  • 1 - Success
  • -1 - Connection Refused.
  • -2 - Connection Timeout (the connection timeout was exceeded before a TCP connection could be established)
  • -3 - Transfer Timeout (the curl timeout was exceeded before the last byte was received). The retcode_pv may be set to 200 or 0, depending whether a 200 OK was received or not. If it was, the body_pv will contain partially downloaded data, use at your own risk! (we recommend you only use this data for logging / debugging purposes)
  • -10 - Internal Error (out of memory, unexpected libcurl error, etc.)

This function can be used from any route.

rest_post usage
...
# Creating a resource using a RESTful service with an HTTP POST request
$var(rc) = rest_post("https://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) != 200) {
xlog("rest_post() rcode=$var(rcode), user=$fU\n");
send_reply("500", "Server Internal Error 2");
exit;
}
...

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

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

Perform a blocking HTTP PUT on the given 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 (the default is “application/x-www-form-urlencoded”)

Similar to rest post, the send_body_pv parameter can also accept a format-string but it cannot be larger than 1024 bytes. For larger messages, you must build them in a pseudo-variable and pass it to the function.

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 will retain the status code of the HTTP response. A 0 status code value means no HTTP reply arrived at all.

Parameter Types

  • url, send_body_pv, send_type_pv - string, pseudo-variable or pseudo-variable format string
  • body_pv, ctype_pv, retcode_pv - pseudo-variables

Return Codes

  • 1 - Success
  • -1 - Connection Refused.
  • -2 - Connection Timeout (the connection timeout was exceeded before a TCP connection could be established)
  • -3 - Transfer Timeout (the curl timeout was exceeded before the last byte was received). The retcode_pv may be set to 200 or 0, depending whether a 200 OK was received or not. If it was, the body_pv will contain partially downloaded data, use at your own risk! (we recommend you only use this data for logging / debugging purposes)
  • -10 - Internal Error (out of memory, unexpected libcurl error, etc.)

This function can be used from any route.

rest_put usage
...
# Creating/Updating a resource using a RESTful service with an HTTP PUT request
$var(rc) = rest_put("https://myserver.org/users/$fU",
"$var(userinfo)", , "$var(body)", "$var(ct)", "$var(rcode)");
if ($var(rc) < 0) {
xlog("rest_put() failed with $var(rc), user=$fU\n");
send_reply("500", "Server Internal Error 3");
exit;
}
if ($var(rcode) != 200) {
xlog("rest_put() rcode=$var(rcode), user=$fU\n");
send_reply("500", "Server Internal Error 4");
exit;
}
...

Append txt to the HTTP headers of the subsequent request. Multiple headers can be appended by making multiple calls before executing a request.

The contents of txt should adhere to the specification for HTTP headers (ex. Field: Value)

Parameter types

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

This function can be used from any route.

rest_append_hf usage
...
# Example of querying a REST service requiring additional headers
rest_append_hf("Authorization: Bearer mF_9.B5f-4.1JqM");
$var(rc) = rest_get("http://getcredit.org/?account=$fU", "$var(credit)");
...

Force a specific TLS certificate and private key pair to be eventually used during the next request. Refer to the tls_mgm module for additional info regarding TLS client domains.

If using this function, you must also ensure that tls_mgm is loaded and properly configured.

Note that if you want to use this feature, the certificate must be provisioned in the configuration file, NOT in the database. In case you are loading TLS certificates from the database, you must at least define the default domain in the configuration script, where is currently the only place you can define it.

Parameter types

  • tls_client_domain - string, pseudo-variable, or a string which includes pseudo-variables.

This function can be used from any route.

rest_init_client_tls usage
...
rest_init_client_tls("1=10.11.12.13:3306");
if (!rest_get("https://example.com"))
xlog("query failed\n");
...

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

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

Perform an asynchronous HTTP GET. This function behaves exactly the same as rest get (in terms of input, output and processing), but in a non-blocking manner. 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) != 200) {
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]]])”

Perform an asynchronous HTTP POST. This function behaves exactly the same as rest post (in terms of input, output and processing), but in a non-blocking manner. 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) != 200) {
xlog("async rest_post() rcode=$var(rcode), user=$fU\n");
send_reply("500", "Server Internal Error 2");
exit;
}
...
}

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

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

Perform an asynchronous HTTP PUT. This function behaves exactly the same as rest put (in terms of input, output and processing), but in a non-blocking manner. Script execution is suspended until the entire content of the HTTP response is available.

async rest_put usage
route {
...
async(rest_put("http://myserver.org/users/$fU", "$var(userinfo)", ,
"$var(body)", "$var(ct)", "$var(rcode)"), resume);
}
route [resume] {
$var(rc) = $rc;
if ($var(rc) < 0) {
xlog("async rest_put() failed with $var(rc), user=$fU\n");
send_reply("500", "Server Internal Error 3");
exit;
}
if ($var(rcode) != 200) {
xlog("async rest_put() rcode=$var(rcode), user=$fU\n");
send_reply("500", "Server Internal Error 4");
exit;
}
...
}

doc copyrights:

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

#NameDevScoreCommitsLines++Lines—
1.Liviu Chircu (@liviuchircu)1236435171722
2.Ionut Ionita (@ionutrazvanionita)2312668267
3.Razvan Crainea (@razvancrainea)15134117
4.Stefan Darius (@dariusstefan)134730120
5.Bogdan-Andrei Iancu (@bogdan-iancu)6410153
6.Vlad Patrascu (@rvlad-patrascu)64551
7.Jarrod Baumann (@jarrodb)6313637
8.Agalya Ramachandran (@AgalyaR)623574
9.Ryan Bullock (@rrb3942)529783
10.John Burke (@john08burke)312626

All remaining contributors: Andrey Vorobiev (@andrey-vorobiev).

(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 - Apr 2021
4.John Burke (@john08burke)Apr 2021 - Apr 2021
5.Vlad Patrascu (@rvlad-patrascu)May 2017 - Jun 2020
6.Bogdan-Andrei Iancu (@bogdan-iancu)Oct 2014 - Jun 2018
7.Ionut Ionita (@ionutrazvanionita)Feb 2017 - Mar 2017
8.Andrey Vorobiev (@andrey-vorobiev)Feb 2017 - Feb 2017
9.Ryan Bullock (@rrb3942)Jan 2017 - Jan 2017
10.Agalya Ramachandran (@AgalyaR)Oct 2016 - Oct 2016

All remaining contributors: Jarrod Baumann (@jarrodb).

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

Last edited by: Razvan Crainea (@razvancrainea), Liviu Chircu (@liviuchircu), Bogdan-Andrei Iancu (@bogdan-iancu), Agalya Ramachandran (@AgalyaR), Jarrod Baumann (@jarrodb).

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