File tree Expand file tree Collapse file tree 2 files changed +52
-4
lines changed Expand file tree Collapse file tree 2 files changed +52
-4
lines changed Original file line number Diff line number Diff line change 1
1
How to use the NIC:
2
2
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
+ ```
Original file line number Diff line number Diff line change @@ -48,13 +48,30 @@ minetest.register_node("digistuff:nic", {
48
48
action = function (pos ,node ,channel ,msg )
49
49
local meta = minetest .get_meta (pos )
50
50
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
52
65
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"
56
69
},
57
70
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
58
75
digilines .receptor_send (pos , digilines .rules .default , channel , res )
59
76
end )
60
77
end
You can’t perform that action at this time.
0 commit comments