Skip to content

Commit 126aa82

Browse files
Add nic format option for json response (#17)
* add nic option config for json response and headers * remove header stuff Co-authored-by: BuckarooBanzay <BuckarooBanzay@users.noreply.github.com>
1 parent ce8f330 commit 126aa82

File tree

2 files changed

+52
-4
lines changed

2 files changed

+52
-4
lines changed

docs/nic.txt

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,33 @@
11
How to use the NIC:
22
Send a digilines signal with the URL you want to download. The HTTPRequestResult table will be sent back on the same channel.
3+
4+
# Examples
5+
6+
## GET request with a plain url
7+
```lua
8+
-- request
9+
digiline_send("nic", "http://example.com")
10+
-- response
11+
event.msg = {
12+
code = 200,
13+
succeeded = true,
14+
data = "<html></html>"
15+
}
16+
```
17+
18+
## GET request with parsed json response
19+
```lua
20+
-- request
21+
digiline_send("nic", {
22+
url = "http://example.com",
23+
parse_json = true
24+
})
25+
-- response
26+
event.msg = {
27+
code = 200,
28+
succeeded = true,
29+
data = {
30+
my = "data"
31+
}
32+
}
33+
```

nic.lua

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -48,13 +48,30 @@ minetest.register_node("digistuff:nic", {
4848
action = function(pos,node,channel,msg)
4949
local meta = minetest.get_meta(pos)
5050
if meta:get_string("channel") ~= channel then return end
51-
if type(msg) ~= "string" then return end
51+
local url
52+
local parse_json = false
53+
-- parse message
54+
if type(msg) == "string" then
55+
-- simple string data
56+
url = msg
57+
elseif type(msg) == "table" and type(msg.url) == "string" then
58+
-- config object
59+
url = msg.url
60+
parse_json = msg.parse_json
61+
else
62+
-- not supported
63+
return
64+
end
5265
http.fetch({
53-
url = msg,
54-
timeout = 5,
55-
user_agent = "Minetest Digilines Modem",
66+
url = url,
67+
timeout = 5,
68+
user_agent = "Minetest Digilines Modem"
5669
},
5770
function(res)
71+
if type(res.data) == "string" and parse_json then
72+
-- parse json data and replace payload
73+
res.data = minetest.parse_json(res.data)
74+
end
5875
digilines.receptor_send(pos, digilines.rules.default, channel, res)
5976
end)
6077
end

0 commit comments

Comments
 (0)