Merge branch 'master' of gitee.com:openLuat/LuatOS
[LuatOS.git] / demo / httpplus / main.lua
blob27309b75d93f899bc863c4562ec961217d855c42
2 -- LuaTools需要PROJECT和VERSION这两个信息
3 PROJECT = "httpdemo"
4 VERSION = "1.0.0"
6 --[[
7 本demo需要socket库, 大部分能联网的设备都具有这个库
8 socket是内置库, 无需require
9 ]]
11 -- sys库是标配
12 _G.sys = require("sys")
13 httpplus = require "httpplus"
16 -- Air780E的AT固件默认会为开机键防抖, 导致部分用户刷机很麻烦
17 if rtos.bsp() == "EC618" and pm and pm.PWK_MODE then
18 pm.power(pm.PWK_MODE, false)
19 end
22 sys.taskInit(function()
23 -----------------------------
24 -- 统一联网函数, 可自行删减
25 ----------------------------
26 if wlan and wlan.connect then
27 -- wifi 联网, ESP32系列均支持
28 local ssid = "luatos1234"
29 local password = "12341234"
30 log.info("wifi", ssid, password)
31 -- TODO 改成esptouch配网
32 -- LED = gpio.setup(12, 0, gpio.PULLUP)
33 wlan.init()
34 wlan.setMode(wlan.STATION)
35 wlan.connect(ssid, password, 1)
36 local result, data = sys.waitUntil("IP_READY", 30000)
37 log.info("wlan", "IP_READY", result, data)
38 device_id = wlan.getMac()
39 elseif rtos.bsp() == "AIR105" then
40 -- w5500 以太网, 当前仅Air105支持
41 w5500.init(spi.HSPI_0, 24000000, pin.PC14, pin.PC01, pin.PC00)
42 w5500.config() --默认是DHCP模式
43 w5500.bind(socket.ETH0)
44 -- LED = gpio.setup(62, 0, gpio.PULLUP)
45 sys.wait(1000)
46 -- TODO 获取mac地址作为device_id
47 elseif mobile then
48 -- Air780E/Air600E系列
49 --mobile.simid(2)
50 -- LED = gpio.setup(27, 0, gpio.PULLUP)
51 device_id = mobile.imei()
52 log.info("ipv6", mobile.ipv6(true))
53 sys.waitUntil("IP_READY", 30000)
54 elseif http then
55 sys.waitUntil("IP_READY")
56 else
57 while 1 do
58 sys.wait(1000)
59 log.info("http", "当前固件未包含http库")
60 end
61 end
62 log.info("已联网")
63 sys.publish("net_ready")
64 end)
66 function test_httpplus()
67 sys.waitUntil("net_ready")
68 -- 调试开关
69 httpplus.debug = true
71 -- socket.sslLog(3)
72 -- local code, resp =httpplus.request({method="POST", url="https://abc:qq@whoami.k8s.air32.cn/goupupup"})
73 -- log.info("http", code, resp)
75 -- 预期返回302
76 -- local code, resp = httpplus.request({method="POST", url="https://air32.cn/goupupup"})
77 -- log.info("http", code, resp)
79 -- local code, resp = httpplus.request({method="POST", url="https://httpbin.air32.cn/post", files={abcd="/luadb/libfastlz.a"}})
80 -- log.info("http", code, resp)
82 -- local code, resp = httpplus.request({method="POST", url="https://httpbin.air32.cn/anything", forms={abcd="12345"}})
83 -- log.info("http", code, resp)
85 -- local code, resp = httpplus.request({method="POST", url="https://httpbin.air32.cn/post", files={abcd="/luadb/abc.txt"}})
86 -- log.info("http", code, resp)
88 -- 简单GET请求
89 local code, resp = httpplus.request({url="https://httpbin.air32.cn/"})
90 log.info("http", code, resp)
92 -- 简单POST请求
93 -- local code, resp = httpplus.request({url="https://httpbin.air32.cn/post", body="123456", method="POST"})
94 -- log.info("http", code, resp)
96 -- 文件上传
97 -- local code, resp = httpplus.request({url="https://httpbin.air32.cn/post", files={myfile="/luadb/abc.txt"}})
98 -- log.info("http", code, resp)
100 -- 自定义header的GET请求
101 -- local code, resp = httpplus.request({url="https://httpbin.air32.cn/get", headers={Auth="12312234"}})
102 -- log.info("http", code, resp)
104 -- 带鉴权信息的GET请求
105 -- local code, resp = httpplus.request({url="https://wendal:123@httpbin.air32.cn/get", headers={Auth="12312234"}})
106 -- log.info("http", code, resp)
108 -- PUT请求
109 -- local code, resp = httpplus.request({url="https://httpbin.air32.cn/put", method="PUT", body="123"})
110 -- log.info("http", code, resp)
112 -- 表单POST
113 -- local code, resp = httpplus.request({url="https://httpbin.air32.cn/post", forms={abc="123"}})
114 -- log.info("http", code, resp)
116 -- 响应体chucked编码测试
117 local code, resp = httpplus.request({url="https://httpbin.air32.cn/stream/1"})
118 log.info("http", code, resp)
119 if code == 200 then
120 log.info("http", "headers", json.encode(resp.headers))
121 local body = resp.body:query()
122 log.info("http", "body", body:toHex())
123 log.info("http", "body", body)
124 log.info("http", "body", json.decode(body:trim()))
125 -- log.info("http", "body", json.decode(resp.body))
129 sys.taskInit(test_httpplus)
131 -- 用户代码已结束---------------------------------------------
132 -- 结尾总是这一句
133 sys.run()
134 -- sys.run()之后后面不要加任何语句!!!!!