Implement support for more arbitrary HTTP methods. Allows GET PUT and POST. With minor changes needed to support others
This commit is contained in:
@@ -1,4 +1,4 @@
|
||||
return function (connection, args)
|
||||
return function (connection, req, args)
|
||||
connection:send("HTTP/1.0 200 OK\r\nContent-Type: text/html\r\nCache-Control: private, no-store\r\n\r\n")
|
||||
connection:send('<!DOCTYPE html><html lang="en"><head><meta charset="utf-8"><title>Arguments</title></head>')
|
||||
connection:send('<body>')
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
return function (connection, args)
|
||||
return function (connection, req, args)
|
||||
connection:send("HTTP/1.0 200 OK\r\nContent-Type: text/html\r\nCache-Control: private, no-store\r\n\r\n")
|
||||
connection:send('<!DOCTYPE html><html lang="en"><head><meta charset="utf-8"><title>Server File Listing</title></head>')
|
||||
connection:send('<body>')
|
||||
|
||||
@@ -21,7 +21,7 @@ local function pushTheButton(connection, pin)
|
||||
|
||||
end
|
||||
|
||||
return function (connection, args)
|
||||
return function (connection, req, args)
|
||||
print('Garage door button was pressed!', args.door)
|
||||
if args.door == "1" then pushTheButton(connection, 1) -- GPIO1
|
||||
elseif args.door == "2" then pushTheButton(connection, 2) -- GPIO2
|
||||
|
||||
@@ -22,7 +22,7 @@
|
||||
<li><a href="index.html">Index</a>: This page (static)</li>
|
||||
<li><a href="zipped.html.gz">Zipped</a>: A compressed file (static)</li>
|
||||
<li><a href="args.lua">Arguments</a>: Parses arguments passed in the URL and prints them. (Lua)</li>
|
||||
<li><a href="post.html">Post</a>: A form that uses POST method, should error. (static)</li>
|
||||
<li><a href="post.lua">Post</a>: A form that uses POST method. Displays different content based on HTTP method. (Lua)</li>
|
||||
<li><a href="garage_door_opener.html">Garage door opener</a>: Control GPIO lines via the server. (Lua)</li>
|
||||
<li><a href="node_info.lua">NodeMCU info</a>: Shows some basic NodeMCU(Lua)</li>
|
||||
<li><a href="file_list.lua">List all server files</a>: Displays a list of all the server files. (Lua)</li>
|
||||
|
||||
@@ -7,7 +7,7 @@ local function sendAttr(connection, attr, val)
|
||||
connection:send("<li><b>".. attr .. ":</b> " .. val .. "<br></li>\n")
|
||||
end
|
||||
|
||||
return function (connection, args)
|
||||
return function (connection, req, args)
|
||||
collectgarbage()
|
||||
sendHeader(connection)
|
||||
connection:send('<!DOCTYPE html><html lang="en"><head><meta charset="utf-8"><title>A Lua script sample</title></head><body><h1>Node info</h1><ul>')
|
||||
|
||||
@@ -1,7 +0,0 @@
|
||||
<!DOCTYPE html>
|
||||
<html lang="en"><head><meta charset="utf-8"><title>Post</title></head><body>
|
||||
<h1>Post</h1>
|
||||
This form uses POST method which is not supported by nodemcu-httpserver.<br>
|
||||
You should get an error when you press submit.
|
||||
<form method="POST"><input type="submit" value="Submit"></form>
|
||||
</body></html>
|
||||
33
http/post.lua
Normal file
33
http/post.lua
Normal file
@@ -0,0 +1,33 @@
|
||||
return function (connection, req, args)
|
||||
connection:send("HTTP/1.0 200 OK\r\nContent-Type: text/html\r\nCache-Control: private, no-store\r\n\r\n")
|
||||
connection:send('<!DOCTYPE html><html lang="en"><head><meta charset="utf-8"><title>Arguments</title></head>')
|
||||
connection:send('<body>')
|
||||
connection:send('<h1>Arguments</h1>')
|
||||
|
||||
local form = [===[
|
||||
<form method="POST">
|
||||
First name:<br><input type="text" name="firstName"><br>
|
||||
Last name:<br><input type="text" name="lastName"><br>
|
||||
<input type="radio" name="sex" value="male" checked>Male<input type="radio" name="sex" value="female">Female<br>
|
||||
<input type="submit" value="Submit">
|
||||
</form>
|
||||
]===]
|
||||
|
||||
if req.method == "GET" then
|
||||
connection:send(form)
|
||||
elseif req.method == "POST" then
|
||||
local rd = req.getRequestData()
|
||||
-- connection:send(cjson.encode(rd))
|
||||
connection:send('<h2>Received the following values:</h2>')
|
||||
connection:send("<ul>\n")
|
||||
for name, value in pairs(rd) do
|
||||
connection:send('<li><b>' .. name .. ':</b> ' .. tostring(value) .. "<br></li>\n")
|
||||
end
|
||||
|
||||
connection:send("</ul>\n")
|
||||
else
|
||||
connection:send("NOT IMPLEMENTED")
|
||||
end
|
||||
|
||||
connection:send('</body></html>')
|
||||
end
|
||||
Reference in New Issue
Block a user