1 Star 0 Fork 379

长风 / LuatOS

forked from 合宙Luat / LuatOS 
加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
luat_lib_http.md 3.68 KB
一键复制 编辑 原始数据 按行查看 历史
module summary version date
http
执行http请求
1.0
2020.07.07

http.req

http.req(url, params, cb)

发起一个http请求,推荐用http.get/post/put/delete方法

参数表

Name Type Description
url string 目标URL,需要是https://或者http://开头,否则将当成http://开头
params table 可选参数. method方法,headers请求头,body数据,ca证书路径,timeout超时时长,
cb function 回调方法

返回值

boolean: 成功启动返回true,否则返回false.启动成功后,cb回调必然会调用一次

调用示例

-- GET请求
http.req("http://www.baidu.com/", nil, functon(ret, code, headers, body)
    log.info("http", ret, code, header, body)
end) 

http.get

http.get(url, params, cb)

发起一个http get请求

参数表

Name Type Description
url string 目标URL,需要是https://或者http://开头,否则将当成http://开头
params table 可选参数. headers请求头,body数据,ca证书路径,timeout超时时长,
cb function 回调方法

返回值

boolean: 成功启动返回true,否则返回false.启动成功后,cb回调必然会调用一次

调用示例

-- GET请求
http.get("http://www.baidu.com/", nil, functon(ret, code, headers, body)
    log.info("http", ret, code, header, body)
end) 

http.post

http.post(url, params, cb)

发起一个http post请求

参数表

Name Type Description
url string 目标URL,需要是https://或者http://开头,否则将当成http://开头
params table 可选参数. headers请求头,body数据,ca证书路径,timeout超时时长,
cb function 回调方法

返回值

boolean: 成功启动返回true,否则返回false.启动成功后,cb回调必然会调用一次

调用示例

-- POST请求
http.post("http://www.baidu.com/", {body=json.encode(data),headers=["Content-Type","application/json"]}, functon(ret, code, headers, body)
    log.info("http", ret, code, header, body)
end) 

http.put

http.put(url, params, cb)

发起一个http put请求

参数表

Name Type Description
url string 目标URL,需要是https://或者http://开头,否则将当成http://开头
params table 可选参数. headers请求头,body数据,ca证书路径,timeout超时时长,
cb function 回调方法

返回值

boolean: 成功启动返回true,否则返回false.启动成功后,cb回调必然会调用一次

调用示例

-- PUT请求
http.put("http://www.baidu.com/", {body=json.encode(data),headers=["Content-Type","application/json"]}, functon(ret, code, headers, body)
    log.info("http", ret, code, header, body)
end) 

http.delete

http.delete(url, params, cb)

发起一个http delete请求

参数表

Name Type Description
url string 目标URL,需要是https://或者http://开头,否则将当成http://开头
params table 可选参数. headers请求头,body数据,ca证书路径,timeout超时时长,
cb function 回调方法

返回值

boolean: 成功启动返回true,否则返回false.启动成功后,cb回调必然会调用一次

调用示例

-- DELETE请求
http.put("http://www.baidu.com/", nil, functon(ret, code, headers, body)
    log.info("http", ret, code, header, body)
end) 
Lua
1
https://gitee.com/LCF228/LuatOS.git
git@gitee.com:LCF228/LuatOS.git
LCF228
LuatOS
LuatOS
master

搜索帮助