test_get.lua 966 B

12345678910111213141516171819
  1. function demo_http_get()
  2. -- 最普通的Http GET请求
  3. local code, headers, body = http.request("GET", "https://httpbin.air32.cn/get").wait()
  4. log.info("http.get", code, headers, body)
  5. -- local code, headers, body = http.request("GET", "https://mirrors6.tuna.tsinghua.edu.cn/", nil, nil, {ipv6=true}).wait()
  6. -- log.info("http.get", code, headers, body)
  7. sys.wait(100)
  8. -- local code, headers, body = http.request("GET", "https://httpbin.air32.cn/bytes/16").wait()
  9. -- log.info("http.get", code, headers, body and #body)
  10. -- 按需打印
  11. -- code 响应值, 若大于等于 100 为服务器响应, 小于的均为错误代码
  12. -- headers是个table, 一般作为调试数据存在
  13. -- body是字符串. 注意lua的字符串是带长度的byte[]/char*, 是可以包含不可见字符的
  14. -- log.info("http", code, json.encode(headers or {}), #body > 512 and #body or body)
  15. -- socket.sntp()
  16. -- sys.wait(100000)
  17. end