Mod curl
From FreeSWITCH Wiki
Contents |
mod_curl (HTTP request API)
mod_curl allows one to make a http request and receive the response. Output can be plain text (headers optional) or a json object.
Installing
To use mod_curl:
Tell FreeSWITCH to compile in this module by editing modules.conf in /usr/src/freeswitch/trunk and uncomment:
#applications/mod_curl
Now go recompile FreeSWITCH.
make make install
Tell FreeSWITCH to actually use the curl module when running by adding the module to modules.conf.xml in /usr/local/freeswitch/conf/autoload_configs:
<load module="mod_curl"/>
There is no separate config file for mod_curl.
Now load up FreeSWITCH!
Application
The syntax is:
<action application="curl" data="url [headers|json] [get|head|post [url_encode_data]]"/>
The curl application sets the variables curl_response_data and curl_response_code. curl_response_data can also be headers/body or json if requested.
<action application="curl" data="http://www.google.com"/> <action application="info"/> <action application="curl" data="http://www.google.com headers"/> <action application="info"/> <action application="curl" data="http://www.google.com json"/> <action application="info"/>
CLI / API
The CLI uses the api interface. The syntax is:
curl url [headers|json] [get|head|post [url_encode_data]]
From the commandline line issue:
curl http://www.google.com
This will return google's home page.
curl http://www.google.com/ headers
Will give you the headers followed by the body. And
curl http://www.google.com/ json
Will give you the headers and body in a structured json stream.
Send POST and GET request.
curl http://www.myhost.com/?get=myGetValue curl http://www.myhost.com post post=myPostValue%20a%20space
Mix with headers or JSON.
curl http://www.myhost.com/?get=myGetValue json curl http://www.myhost.com headers post post=myPostValue%20a%20space
Lua Usage
Note: it is good practice to check session:ready() before any long function calls such as a HTTP request so that your script stops running and releases its resources as soon as possible if the call is hung up.
This shows how to do a GET request:
session:execute("curl", "http://www.myhost.com/?name1=value1&name2=value2")
curl_response_code = session:getVariable("curl_response_code")
curl_response = session:getVariable("curl_response_data")
This shows how to do a POST request:
session:execute("curl", "http://www.myhost.com/ post name1=value1&name2=value2")
curl_response_code = session:getVariable("curl_response_code")
curl_response = session:getVariable("curl_response_data")
You can also use the API interface:
api = freeswitch.API();
get_response = api:execute("curl", "http://www.myhost.com/?name1=value1&name2=value2")
post_response = api:execute("curl", "http://www.myhost.com/ post name1=value1&name2=value2")
In all the above examples, the submitted values must be URL encoded.
Submit: first = "a short value" second = "a slightly longer value" as: first=a%20short%20value&second=a%20slightly%20longer%20value
This shows one method of encoding parameters to form a GET/POST request: (or use CGILua's urlcode)
function codificaParametros(paramsT)
function escape (s)
s = string.gsub(
s,
'([\r\n"#%%&+:;<=>?@^`{|}%\\%[%]%(%)$!~,/\'])',
function (c)
return '%'..string.format("%X", string.byte(c))
end
)
s = string.gsub(s, "%s", "+")
return s
end
function encode (t)
local s = ""
for k , v in pairs(t) do
s = s .. "&" .. escape(k) .. "=" .. escape(v)
end
return string.sub(s, 2) -- remove first `&'
end
if type(paramsT) == 'table' then
return encode(paramsT)
else
local tmp = Utils:commaText(paramsT, '&');
local myParamsT = {};
for k, v in pairs(tmp) do
local pos = 0
pos = string.find(v, '=')
if not pos then return '' end
myParamsT[string.sub(v, 1, pos-1 )] = string.sub(v, pos+1 )
end
return encode(myParamsT)
end
end
local paramsT = {
param1 = 'valor1',
param2 = '\nh@la<>?'
}
local encode = codificaParametros (paramsT)
session:answer()
print (string.format("\n%s\n", encode));
--param1=valor1¶m2=%Ah%40la%3C%3E%3F
session:hangup();

