openwrt web开发-luci http常用接口

luci http接口

luci是一套web框架,虽然对http协议进行了封装,开发人员可以不用关心具体http底层如何处理,但是我们还是需要用到http请求的一些接口,比如我们在自定义http请求接口时,需要获取http请求的表单参数、cookies等,有时候也需要处理上传的文件、url重定向。 这一节列出http常用的一些操作接口,可以根据实际开发需要进行学习。

接口列表

接口说明
luci.http.formvalue(param)获取表单提交的数据,支持GET和POST
luci.http.content()Return the request content if the request was of unknown type.
luci.http.getcookie(name)获取cookie值
luci.http.getenv(name)获取环境变量
luci.http.prepare_content()设置content-type,如luci.http.prepare_content(“application/json”)
luci.http.source()Get the RAW HTTP input source
luci.http.write(content)返回数据,content为字符串类型
luci.http.write_json(object)返回json格式数据,object为对象,会自动转换成json格式
luci.http.redirect(url)重定向到指定url
luci.http.urldecode(str)Return the URL-decoded equivalent of a string.
luci.http.urlencode(str)Return the URL-encoded equivalent of a string.
luci.http.setfilehandler (callback)Set a handler function for incoming user file uploads.

如何获取http请求的参数

说明

在定义接口时,我们有时候需要定义请求参数,用于通过参数判断获取哪些内容,或者用于提交修改的数据

接口

luci.http.formvalue(name)

参数说明

参数为可选的,如果不指明参数名称,会返回所有的参数

实例1

获取所有GET参数

源码

module("luci.controller.ctrl_call_test3", package.seeall)
function index()
	entry({"api", "system", "param_test"}, call("action_param_test"), nil).dependent=false
end
function action_param_test()
	local param = luci.http.formvalue()
	luci.http.prepare_content("application/json")
	if param == nil
	then
		luci.http.write_json({})
	else
		luci.http.write_json(param)
	end
end

请求

http://192.168.10.180/cgi-bin/luci/api/system/param_test?name=derry&id=1001&class=123

结果

{ “id”: “1001”, “class”: “123”, “name”: “derry” }

实例2

获取指定名称的GET参数

源码

module("luci.controller.ctrl_call_test3", package.seeall)
function index()
	entry({"api", "system", "param_test2"}, call("action_param_test2"), nil).dependent=false
end
function action_param_test2()
	local name = luci.http.formvalue("name")
	luci.http.prepare_content("application/json")
	if name == nil
	then
		luci.http.write_json({})
	else
		luci.http.write_json(name)
	end
end

请求

http://192.168.10.180/cgi-bin/luci/api/system/param_test2?name=derry&id=1001&class=123

结果

"derry"

获取http请求环境变量

说明

显示http请求所有环境变量,包含客户端ip、url、端口号、cookie等

源码

function action_show_env()
	luci.http.write_json(luci.http.getenv())
end

请求 http://192.168.10.180/cgi-bin/luci/api/system/show_env 结果

{
  "HTTP_ACCEPT": "text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9",
  "SCRIPT_NAME": "/cgi-bin/luci",
  "QUERY_STRING": "",
  "HTTP_ACCEPT_ENCODING": "gzip, deflate",
  "SERVER_ADDR": "192.168.10.180",
  "GATEWAY_INTERFACE": "CGI/1.1",
  "REMOTE_ADDR": "192.168.10.51",
  "SERVER_PORT": "80",
  "SCRIPT_FILENAME": "/www/cgi-bin/luci",
  "REQUEST_URI": "/cgi-bin/luci/api/system/show_env",
  "SERVER_PROTOCOL": "HTTP/1.1",
  "REMOTE_HOST": "192.168.10.51",
  "REMOTE_PORT": "58470",
  "REDIRECT_STATUS": "200",
  "PATH": "/sbin:/usr/sbin:/bin:/usr/bin",
  "SERVER_NAME": "192.168.10.180",
  "SERVER_SOFTWARE": "uhttpd",
  "HTTP_COOKIE": "sysauth=f8ebe23df68d6afabc14bf136fbda335",
  "HTTP_HOST": "192.168.10.180",
  "HTTP_ACCEPT_LANGUAGE": "zh-CN,zh;q=0.9",
  "DOCUMENT_ROOT": "/www",
  "PATH_INFO": "/api/system/show_env",
  "HTTP_USER_AGENT": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/95.0.4638.54 Safari/537.36",
  "HTTP_CONNECTION": "keep-alive",
  "REQUEST_METHOD": "GET"
}

重定向到指定网址

说明

在请求某个接口时,我们有时候需要重定向到指定网址或页面,这里就可以用redirect接口实现

源码

请求 http://192.168.10.180/cgi-bin/luci/api/system/redirect

function action_redirect()
	luci.http.redirect("https://www.baidu.com")
end

结果 当在浏览器访问以上链接时,实际显示的是www.baidu.com 首页

如需转载请保留该博客链接!OpenWrt flashing tutorials, firmware, routers, ipks and apps - ttcoder » openwrt web开发-luci http常用接口

相关文章

评论 (3)

3 + 2 =

  1. avatar

    I was excited to uncover this page. I need to to
    thank you for your time due to this wonderful read!! I definitely
    savored every bit of it and I have you bookmarked to check out new information on your blog.

  2. avatar

    Excellent article! We will be linking to this particularly great content
    on our site. Keep up the good writing.

  3. avatar

    This is very interesting, You're a very skilled blogger.
    I've joined your rss feed and look forward to
    seeking more of your magnificent post. Also, I have shared your website in my social
    networks!