http.req(method, url, param, cb)
http.get(url, param, cb)
http.post(url, param, cb)
http.put(url, param, cb)
http.delete(url, param, cb)
异步http请求
入参 |
说明 |
method |
string, 方法,GET , POST ,PUT ,DELETE ,等效于单独调用下面的函数 |
url |
string,连接 |
param |
table,参数,可以为空;支持的参数path ,sbl ,header ,type ,body ,timeout |
cb |
function,执行完的回调函数 |
返回 |
说明 |
res |
bool,创建请求成功或者失败 |
local respHandler = function(ret, code, headers, body)
-- body
log.debug("HTTP recv over", ret, code, body)
sys.publish("HTTP_OVER")
end
log.info("test http get")
----------------------------
local param = {
timeout = 15, -- 单位秒
body = nil, -- get没有body
type = 1 -- body参数类型,1表单,2json
}
-- http.get("http://www.baidu.com", param, respHandler)
-- -- 同步等待
-- sys.waitUntil("HTTP_OVER", 10000)
-- sys.wait(5000)
-- ----------------------------
param = {
timeout = 15, -- 单位秒
body = table.urlEncode({ --table.urlEncode定义在utils.lua里面
project_key = "" .. productKey,
imei = "" .. misc.getImei(),
}),
type = 1 -- body参数类型,1表单,2json
}
log.info("test http get2 with url query")
local url =
"https://www.baidu.com"
http.req("POST", url, param, respHandler)
-- 同步等待
sys.waitUntil("HTTP_OVER", 10000)
sys.wait(5000)
-- ----------------------------
log.info("test http post")
url = "https://www.baidu.com"
param = {
timeout = 15, -- 单位秒
body = json.encode({test = 123, aa = "wqfwqf"}), -- post才有body
type = 2 -- body参数类型,1表单,2json
}
http.req("POST", url, param, respHandler)
-- 通过指定path下载文件到flash
param = {
timeout = 15, -- 单位秒
path = "/data/test.txt"
}
log.info("test http get2 with url query")
local url =
"https://www.baidu.com"
http.get(url, param, respHandler)
-- 带header参数
local header = table.setHeader({ Authorization = "test1234" })
local name = misc.getImei()
http.get("http://api.heclouds.com/mqtt/v1/devices/" .. name, {
timeout = 10,
header = header
}, function(ret, code, headers, body)
sys.publish("ONE_HTTP1", code, headers, body)
end)
local ret, code, head, body = sys.waitUntil("ONE_HTTP1", 10000)
if tonumber(code) == 200 and body then
local dat, res, err = json.decode(body)
if res and tonumber(dat.code_no) == 0 then
log.info("返回的MQTTS设备:", dat.data.name)
return dat.data
else
log.error("设备不存在:", body)
end
end
异步http请求
入参 |
说明 |
url |
string,连接 |
param |
table,参数,可以为空;支持的参数path ,sbl ,header ,type ,body ,timeout |
cb |
function,执行完的回调函数 |
返回 |
说明 |
res |
bool,创建请求成功或者失败 |
异步http请求
入参 |
说明 |
url |
string,连接 |
param |
table,参数,可以为空;支持的参数path ,sbl ,header ,type ,body ,timeout |
cb |
function,执行完的回调函数 |
返回 |
说明 |
res |
bool,创建请求成功或者失败 |
异步http请求
入参 |
说明 |
url |
string,连接 |
param |
table,参数,可以为空;支持的参数path ,sbl ,header ,type ,body ,timeout |
cb |
function,执行完的回调函数 |
返回 |
说明 |
res |
bool,创建请求成功或者失败 |
异步http请求
入参 |
说明 |
url |
string,连接 |
param |
table,参数,可以为空;支持的参数path ,sbl ,header ,type ,body ,timeout |
cb |
function,执行完的回调函数 |
返回 |
说明 |
res |
bool,创建请求成功或者失败 |