Skip to content

Commit

Permalink
add: 添加http库配合miniz库解析和风天气API的响应
Browse files Browse the repository at this point in the history
  • Loading branch information
wendal committed Dec 2, 2023
1 parent f876022 commit 1b6441e
Showing 1 changed file with 26 additions and 0 deletions.
26 changes: 26 additions & 0 deletions demo/http/main.lua
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@ VERSION = "1.0.0"
--[[
本demo需要http库, 大部分能联网的设备都具有这个库
http也是内置库, 无需require
1. 如需上传大文件,请使用 httpplus 库, 对应demo/httpplus
2.
]]

-- sys库是标配
Expand Down Expand Up @@ -188,6 +191,27 @@ function demo_http_post_file()
end


local function demo_http_get_gzip()
-- 这里用 和风天气 的API做演示
-- 这个API的响应, 总会gzip压缩过, 需要配合miniz库进行解压
local code, headers, body = http.request("GET", "https://devapi.qweather.com/v7/weather/now?location=101010100&key=0e8c72015e2b4a1dbff1688ad54053de").wait()
log.info("http.gzip", code)
if code == 200 then
local re = miniz.uncompress(body:sub(11), 0)
log.info("和风天气", re)
if re then
local jdata = json.decode(re)
log.info("jdata", jdata)
if jdata then
log.info("和风天气", jdata.code)
if jdata.now then
log.info("和风天气", "天气", jdata.now.text)
log.info("和风天气", "温度", jdata.now.temp)
end
end
end
end
end

sys.taskInit(function()
sys.wait(100)
Expand All @@ -212,6 +236,8 @@ sys.taskInit(function()
-- demo_http_post_file()
-- 文件下载
-- demo_http_download()
-- gzip压缩的响应, 以和风天气为例
-- demo_http_get_gzip()

sys.wait(1000)
-- 打印一下内存状态
Expand Down

0 comments on commit 1b6441e

Please sign in to comment.