http_demo.lua 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. local libnet = require "libnet"
  2. -- local function downloadFile(code, headers, body)
  3. -- log.info("http.get", code, json.encode(headers), body)
  4. -- local r = io.writeFile("/data.txt", body)
  5. -- if r then
  6. -- log.info("文件写入成功")
  7. -- sys.publish("WRITE_FILE_SUCCESS")
  8. -- else
  9. -- log.info("文件写入失败")
  10. -- end
  11. -- end
  12. -- Download File task
  13. sys.taskInit(function ()
  14. sys.wait(5000)
  15. log.info("------下载文件------")
  16. local path = "/data.txt"
  17. -- GET请求,但下载到文件
  18. http.request(
  19. "GET",
  20. "http://cdn.openluat-luatcommunity.openluat.com/attachment/20220825134126812_text.txt").cb
  21. (
  22. function (code, headers, body)
  23. log.info("http.get", code, json.encode(headers), body)
  24. local r = io.writeFile("/data.txt", body)
  25. if r then
  26. log.info("文件写入成功")
  27. sys.publish("WRITE_FILE_SUCCESS")
  28. else
  29. log.info("文件写入失败")
  30. end
  31. end
  32. )
  33. sys.waitUntil("WRITE_FILE_SUCCESS")
  34. local data = io.readFile(path)
  35. if data then
  36. log.info("fs", "data", data, data:toHex())
  37. else
  38. log.info("打开文件失败")
  39. end
  40. -- POST and download, task内的同步操作
  41. -- local opts = {} -- 额外的配置项
  42. -- opts["dst"] = "/data.bin" -- 下载路径,可选
  43. -- opts["adapter"] = "" -- 使用哪个网卡,可选
  44. -- local req_headers = {}
  45. -- req_headers["Content-Type"] = "application/json"
  46. -- local code, headers, body = http.request("POST","http://site0.cn/api/httptest/simple/date",
  47. -- json.encode(req_headers), -- 请求所添加的 headers, 可以是nil
  48. -- "",
  49. -- opts
  50. -- ).wait()
  51. -- log.info("http.post", code, headers, body) -- 只返回code和headers
  52. end)
  53. -- [[
  54. sys.taskInit(
  55. function()
  56. sys.wait(3000)
  57. log.info("mem.lua", rtos.meminfo())
  58. log.info("mem.sys", rtos.meminfo("sys"))
  59. -- GET请求
  60. log.info("------GET请求------")
  61. local code, headers, body = http.request("GET","https://www.baidu.com/").wait()
  62. log.info("http.get", code, json.encode(headers), body)
  63. sys.wait(2000)
  64. -- POST请求后以回调函数方式处理的例子。
  65. log.info("------POST请求------")
  66. local req_headers = {}
  67. req_headers["Content-Type"] = "application/json"
  68. local body = json.encode({name="LuatOS"})
  69. http.request("POST","http://site0.cn/api/httptest/simple/date",
  70. req_headers,
  71. body -- POST请求所需要的body, string, zbuff, file均可
  72. ).cb(function(code, headers, body)
  73. log.info("http.post", code, json.encode(headers), body)
  74. end)
  75. end
  76. )
  77. -- ]]
  78. -- sys.taskInit(
  79. -- function()
  80. -- sys.wait(3000)
  81. -- -- while 1 do
  82. -- log.info("mem.lua", rtos.meminfo())
  83. -- log.info("mem.sys", rtos.meminfo("sys"))
  84. -- local code, headers, body = http.request("GET","https://www.baidu.com/").wait()
  85. -- log.info("http.get", code, json.encode(headers), body)
  86. -- -- -- local code, headers, body = http.request("GET","http://site0.cn/api/httptest/simple/time").wait()
  87. -- -- -- log.info("http.get", code, json.encode(headers), body)
  88. -- -- sys.wait(2000)
  89. -- -- -- POST request
  90. -- -- local req_headers = {}
  91. -- -- req_headers["Content-Type"] = "application/json"
  92. -- -- local body = json.encode({name="LuatOS"})
  93. -- -- local code, headers, body = http.request("POST","http://site0.cn/api/httptest/simple/date",
  94. -- -- req_headers,
  95. -- -- body -- POST请求所需要的body, string, zbuff, file均可
  96. -- -- ).cb(function (code, headers, body)
  97. -- -- log.info("http.post", code, headers, body)
  98. -- -- end)
  99. -- -- log.info("http.post1", code, headers, body)
  100. -- -- end
  101. -- end
  102. -- )