-- Fetch() API Demo
local function WriteForm(url)
Write([[
redbean fetch demo
Your redbean is able to function as an HTTP client too.
Lua server pages can use the Fetch()
API to
to send outgoing HTTP and HTTPS requests to other web
servers. All it takes is a line of code!
]] % {EscapeHtml(url)})
end
local function main()
if IsPublicIp(GetClientAddr()) then
ServeError(403)
elseif GetMethod() == 'GET' or GetMethod() == 'HEAD' then
WriteForm("https://www.cloudflare.com/robots.txt")
elseif GetMethod() == 'POST' then
status, headers, payload = Fetch(GetParam('url'))
WriteForm(GetParam('url'))
Write('\r\n')
Write('- Status\r\n')
Write('
%d %s\r\n' % {status, GetHttpReason(status)})
Write('
- Headers\r\n')
Write('
- \r\n')
for k,v in pairs(headers) do
Write('
')
Write(EscapeHtml(k))
Write(': ')
Write(EscapeHtml(v))
Write('
\r\n')
end
Write(' - Payload\r\n')
Write('
')
Write(EscapeHtml(VisualizeControlCodes(payload)))
Write('
\r\n')
Write('
\r\n')
else
ServeError(405)
SetHeader('Allow', 'GET, HEAD, POST')
end
end
main()