-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathjson_response_example.lua
More file actions
55 lines (44 loc) · 1.35 KB
/
json_response_example.lua
File metadata and controls
55 lines (44 loc) · 1.35 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
--- JSON Response Example
--- Run: lua ./examples/json_response_example.lua
--- Test: curl http://localhost:8081/api/users
local PudimServer = require("PudimServer")
local server = PudimServer:Create{
Port = 8081,
Address = "localhost",
ServiceName = "JSON Example",
Middlewares = {}
}
-- Tables are auto-encoded to JSON with Content-Type: application/json
server:Routes("/api/users", function(req, res)
if req.method == "GET" then
return res:response(200, {
users = {
{ id = 1, name = "Davi" },
{ id = 2, name = "Maria" },
},
total = 2
})
end
if req.method == "POST" then
return res:response(201, { msg = "User created!", data = req.body })
end
return res:response(405, { error = "Method not allowed" })
end)
-- You can also pass explicit Content-Type with a table body
server:Routes("/api/status", function(req, res)
if req.method == "GET" then
return res:response(200, {
status = "ok",
timestamp = os.time(),
service = "JSON Example"
}, {
["Content-Type"] = "application/json",
["X-Custom-Header"] = "PudimServer"
})
end
return res:response(405, { error = "Method not allowed" })
end)
print("JSON Example running at http://localhost:8081")
print("Try: curl http://localhost:8081/api/users")
print("Try: curl http://localhost:8081/api/status")
server:Run()