Re: Another web server
Posted: Sat Jan 10, 2015 11:44 am
Here's an update. This one will parse parameters from a URL-encoded form POST. It also allows you to send a response with a content-type other than HTML, for example json. And I think it will solve the problem jankop ran into with an invalid request causing a reset.
Here's an example showing how to return JSON and how to use parameters sent via a form with POST:
Here's an example showing how to return JSON and how to use parameters sent via a form with POST:
Code: Select all
pages = {}
pages["/name"] = function(request)
return '<html><head><title>Form Test</title></head><body>' ..
'<form method="POST" action="/welcome">' ..
'What is your name? <input name="n"/>' ..
'<input type="submit" />' ..
'</form></body></html>'
end
pages["/welcome"] = function(request)
name = request.form["n"]
return "<html><head><title>Welcome</title></head><body><p>Welcome " .. name .. ".</p></body></html>"
end
pages["/json"] = function(request)
return '{ "temperature": 82, "barometer": 30.2 }', 'application/json'
end
w = startWeb({pages = pages})