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>
This commit is contained in:
Buckaroo Banzai 2021-06-06 09:21:31 +02:00 committed by GitHub
parent ce8f33033e
commit 126aa826bf
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 52 additions and 4 deletions

View File

@ -1,2 +1,33 @@
How to use the NIC:
Send a digilines signal with the URL you want to download. The HTTPRequestResult table will be sent back on the same channel.
# Examples
## GET request with a plain url
```lua
-- request
digiline_send("nic", "http://example.com")
-- response
event.msg = {
code = 200,
succeeded = true,
data = "<html></html>"
}
```
## GET request with parsed json response
```lua
-- request
digiline_send("nic", {
url = "http://example.com",
parse_json = true
})
-- response
event.msg = {
code = 200,
succeeded = true,
data = {
my = "data"
}
}
```

25
nic.lua
View File

@ -48,13 +48,30 @@ minetest.register_node("digistuff:nic", {
action = function(pos,node,channel,msg)
local meta = minetest.get_meta(pos)
if meta:get_string("channel") ~= channel then return end
if type(msg) ~= "string" then return end
local url
local parse_json = false
-- parse message
if type(msg) == "string" then
-- simple string data
url = msg
elseif type(msg) == "table" and type(msg.url) == "string" then
-- config object
url = msg.url
parse_json = msg.parse_json
else
-- not supported
return
end
http.fetch({
url = msg,
timeout = 5,
user_agent = "Minetest Digilines Modem",
url = url,
timeout = 5,
user_agent = "Minetest Digilines Modem"
},
function(res)
if type(res.data) == "string" and parse_json then
-- parse json data and replace payload
res.data = minetest.parse_json(res.data)
end
digilines.receptor_send(pos, digilines.rules.default, channel, res)
end)
end