OpenResty提供了各种API用来获取不同类型的请求参数:
参数格式 | 参数示例 | 参数解析代码示例 |
---|---|---|
路径占位符 | /item/1001 | # 1.正则表达式匹配: location ~ /item/(\d+) { content_by_lua_file lua/item.lua; } – 2. 匹配到的参数会存入ngx.var数组中, – 可以用角标获取 local id = ngx.var[1] |
请求头 | id:1001 | – 获取请求头,返回值是table类型 local headers = ngx.req.get_headers() |
Get请求参数 | ?id=1001 | – 获取GET请求参数,返回值是table类型 local getParams = ngx.req.get_uri_args() |
Post表单参数 | id=1001 | – 读取请求体 ngx.req.read_body() – 获取POST表单参数,返回值是table类型 local postParams = ngx.req.get_post_args() |
JSON参数 | {“id”: 1001} | – 读取请求体 ngx.req.read_body() – 获取body中的json参数,返回值是string类型 local jsonBody = ngx.req.get_body_data() |