Skip to content

Instantly share code, notes, and snippets.

@rsampaio
Last active August 29, 2015 14:14
Show Gist options
  • Save rsampaio/1103af862c752f36a7e9 to your computer and use it in GitHub Desktop.
Save rsampaio/1103af862c752f36a7e9 to your computer and use it in GitHub Desktop.
#!/usr/bin/env lua
--
http = require("socket.http")
string = require("string")
ltn12 = require ("ltn12")
function encodetable(table)
local str = ""
local function char2hexcode(c)
return string.format("%%%02X", string.byte(c))
end
local function escape(str)
str = string.gsub(str, "\n", "\r\n")
str = string.gsub(str, "([^0-9a-zA-Z_. -])", char2hexcode)
str = string.gsub(str, " ", "+")
return str
end
for key, vals in pairs(table) do
if type(vals) ~= "table" then
vals = {vals}
end
for k, val in pairs(vals) do
str = str .. "&" .. escape(key) .. "=" ..escape(val)
end
end
return string.sub(str,2)
end
params = {
['server'] = '0.0.0.0:8085',
['charset_post'] = "utf-8",
['charset'] = 'utf-8',
['pure'] = 1,
['js'] = 0,
['tst'] = 1,
['msg'] = "2 * 4 + 2",
}
data = encodetable(params)
response = {}
body, code, headers, status = http.request ({
method = "POST",
url = "http://www.ed.conpet.gov.br/mod_perl/bot_gateway.cgi",
headers = {
["content-type"] = "application/x-www-form-urlencoded",
["content-length"] = string.len(data),
},
source = ltn12.source.string(data),
sink = ltn12.sink.table(response)
})
print(encodetable(params))
print(response[1])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment