zoukankan      html  css  js  c++  java
  • nginx中lua语言获取传参字符串转json打印key,value

     1 -- 准许获取传参内容
     2 
     3 ngx.req.read_body()
     4 
     5 -- 获取传参内容参数    
     6 
     7 
     8 
     9 local args = ngx.req.get_body_data()
    10 
    11 local cjson = require("cjson");
    12 
    13  
    14 
    15 -- 将body  json格式内容转为json格式   注意:转成json后,为table类型
    16 local json = cjson.decode(args);
    17 
    18 --  打印  json的key,value
    19 printJson(json)
    20 
    21 -- 通过递归,完成多层级tableJson的打印
    22 
    23 function printJson(jsonTable)
    24 
    25   for k, v in pairs(jsonTable) do
    26 
    27     if(type(v) == "table") then
    28 
    29       printJson(v)
    30     else
    31       print(k..":"..v)
    32     end
    33   end
    34 end

    关于nginx中 lua获取方法对照表,转自:https://www.cnblogs.com/yanzi-meng/p/9450991.html

      1 ngx_lua 模块提供的指令和API等:
      2 
      3 指令名称    说明
      4 lua_use_default_type    是否使用default_type指令定义的Content-Type默认值
      5 lua_code_cache    *_by_lua_file文件是否cache
      6 lua_regex_cache_max_entries     
      7 lua_regex_match_limit     
      8 lua_package_path    用Lua写的lua外部库路径(.lua文件)
      9 lua_package_cpath    用C写的lua外部库路径(.so文件)
     10 init_by_lua    master进程启动时挂载的lua代码
     11 init_by_lua_file     
     12 init_worker_by_lua    worker进程启动时挂载的lua代码,常用来执行一些定时器任务
     13 init_worker_by_lua_file     
     14 set_by_lua    设置变量
     15 set_by_lua_file     
     16 content_by_lua    handler模块
     17 content_by_lua_file     
     18 rewrite_by_lua     
     19 rewrite_by_lua_file     
     20 access_by_lua     
     21 access_by_lua_file     
     22 header_filter_by_lua    header filter模块
     23 header_filter_by_lua_file     
     24 body_filter_by_lua    body filter模块,ngx.arg[1]代表输入的chunk,ngx.arg[2]代表当前chunk是否为last
     25 body_filter_by_lua_file     
     26 log_by_lua     
     27 log_by_lua_file     
     28 lua_need_request_body    是否读请求体,跟ngx.req.read_body()函数作用类似
     29 lua_shared_dict    创建全局共享的table(多个worker进程共享)
     30 lua_socket_connect_timeout    TCP/unix 域socket对象connect方法的超时时间
     31 lua_socket_send_timeout    TCP/unix 域socket对象send方法的超时时间
     32 lua_socket_send_lowat    设置cosocket send buffer的low water值
     33 lua_socket_read_timeout    TCP/unix 域socket对象receive方法的超时时间
     34 lua_socket_buffer_size    cosocket读buffer大小
     35 lua_socket_pool_size    cosocket连接池大小
     36 lua_socket_keepalive_timeout    cosocket长连接超时时间
     37 lua_socket_log_errors    是否打开cosocket错误日志
     38 lua_ssl_ciphers     
     39 lua_ssl_crl     
     40 lua_ssl_protocols     
     41 lua_ssl_trusted_certificate     
     42 lua_ssl_verify_depth     
     43 lua_http10_buffering     
     44 rewrite_by_lua_no_postpone     
     45 lua_transform_underscores_in_response_headers     
     46 lua_check_client_abort    是否监视client提前关闭请求的事件,如果打开监视,会调用ngx.on_abort()注册的回调
     47 lua_max_pending_timers     
     48 lua_max_running_timers     
     49  
     50 
     51  
     52 
     53  
     54 
     55  
     56 
     57 table    说明
     58 ngx.arg    指令参数,如跟在content_by_lua_file后面的参数
     59 ngx.var    变量,ngx.var.VARIABLE引用某个变量
     60 ngx.ctx    请求的lua上下文
     61 ngx.header    响应头,ngx.header.HEADER引用某个头
     62 ngx.status    响应码
     63       
     64 API    说明
     65 ngx.log    输出到error.log
     66 print    等价于 ngx.log(ngx.NOTICE, ...)
     67 ngx.send_headers    发送响应头
     68 ngx.headers_sent    响应头是否已发送
     69 ngx.resp.get_headers    获取响应头
     70 ngx.timer.at    注册定时器事件
     71 ngx.is_subrequest    当前请求是否是子请求
     72 ngx.location.capture    发布一个子请求
     73 ngx.location.capture_multi    发布多个子请求
     74 ngx.exec     
     75 ngx.redirect     
     76 ngx.print    输出响应
     77 ngx.say    输出响应,自动添加'
    '
     78 ngx.flush    刷新响应
     79 ngx.exit    结束请求
     80 ngx.eof     
     81 ngx.sleep    无阻塞的休眠(使用定时器实现)
     82 ngx.get_phase     
     83 ngx.on_abort    注册client断开请求时的回调函数
     84 ndk.set_var.DIRECTIVE     
     85 ngx.req.start_time    请求的开始时间
     86 ngx.req.http_version    请求的HTTP版本号
     87 ngx.req.raw_header    请求头(包括请求行)
     88 ngx.req.get_method    请求方法
     89 ngx.req.set_method    请求方法重载
     90 ngx.req.set_uri    请求URL重写
     91 ngx.req.set_uri_args     
     92 ngx.req.get_uri_args    获取请求参数
     93 ngx.req.get_post_args    获取请求表单
     94 ngx.req.get_headers    获取请求头
     95 ngx.req.set_header     
     96 ngx.req.clear_header     
     97 ngx.req.read_body    读取请求体
     98 ngx.req.discard_body    扔掉请求体
     99 ngx.req.get_body_data     
    100 ngx.req.get_body_file     
    101 ngx.req.set_body_data     
    102 ngx.req.set_body_file     
    103 ngx.req.init_body     
    104 ngx.req.append_body     
    105 ngx.req.finish_body     
    106 ngx.req.socket     
    107 ngx.escape_uri    字符串的url编码
    108 ngx.unescape_uri    字符串url解码
    109 ngx.encode_args    将table编码为一个参数字符串
    110 ngx.decode_args    将参数字符串编码为一个table
    111 ngx.encode_base64    字符串的base64编码
    112 ngx.decode_base64    字符串的base64解码
    113 ngx.crc32_short    字符串的crs32_short哈希
    114 ngx.crc32_long    字符串的crs32_long哈希
    115 ngx.hmac_sha1    字符串的hmac_sha1哈希
    116 ngx.md5    返回16进制MD5
    117 ngx.md5_bin    返回2进制MD5
    118 ngx.sha1_bin    返回2进制sha1哈希值
    119 ngx.quote_sql_str    SQL语句转义
    120 ngx.today    返回当前日期
    121 ngx.time    返回UNIX时间戳
    122 ngx.now    返回当前时间
    123 ngx.update_time    刷新时间后再返回
    124 ngx.localtime     
    125 ngx.utctime     
    126 ngx.cookie_time    返回的时间可用于cookie值
    127 ngx.http_time    返回的时间可用于HTTP头
    128 ngx.parse_http_time    解析HTTP头的时间
    129 ngx.re.match     
    130 ngx.re.find     
    131 ngx.re.gmatch     
    132 ngx.re.sub     
    133 ngx.re.gsub     
    134 ngx.shared.DICT     
    135 ngx.shared.DICT.get     
    136 ngx.shared.DICT.get_stale     
    137 ngx.shared.DICT.set     
    138 ngx.shared.DICT.safe_set     
    139 ngx.shared.DICT.add     
    140 ngx.shared.DICT.safe_add     
    141 ngx.shared.DICT.replace     
    142 ngx.shared.DICT.delete     
    143 ngx.shared.DICT.incr     
    144 ngx.shared.DICT.flush_all     
    145 ngx.shared.DICT.flush_expired     
    146 ngx.shared.DICT.get_keys     
    147 ngx.socket.udp     
    148 udpsock:setpeername     
    149 udpsock:send     
    150 udpsock:receive     
    151 udpsock:close     
    152 udpsock:settimeout     
    153 ngx.socket.tcp     
    154 tcpsock:connect     
    155 tcpsock:sslhandshake     
    156 tcpsock:send     
    157 tcpsock:receive     
    158 tcpsock:receiveuntil     
    159 tcpsock:close     
    160 tcpsock:settimeout     
    161 tcpsock:setoption     
    162 tcpsock:setkeepalive     
    163 tcpsock:getreusedtimes     
    164 ngx.socket.connect     
    165 ngx.thread.spawn     
    166 ngx.thread.wait     
    167 ngx.thread.kill     
    168 coroutine.create     
    169 coroutine.resume     
    170 coroutine.yield     
    171 coroutine.wrap     
    172 coroutine.running     
    173 coroutine.status     
    174 ngx.config.debug    编译时是否有 --with-debug选项
    175 ngx.config.prefix    编译时的 --prefix选项
    176 ngx.config.nginx_version    返回nginx版本号
    177 ngx.config.nginx_configure    返回编译时 ./configure的命令行选项
    178 ngx.config.ngx_lua_version    返回ngx_lua模块版本号
    179 ngx.worker.exiting    当前worker进程是否正在关闭(如reload、shutdown期间)
    180 ngx.worker.pid    返回当前worker进程的pid
    181       
    182       
    183       
    184 常量    说明
    185 Core constants    ngx.OK (0)
    186 ngx.ERROR (-1)
    187 ngx.AGAIN (-2)
    188 ngx.DONE (-4)
    189 ngx.DECLINED (-5)
    190 ngx.nil
    191 HTTP method constants    ngx.HTTP_GET
    192 ngx.HTTP_HEAD
    193 ngx.HTTP_PUT
    194 ngx.HTTP_POST
    195 ngx.HTTP_DELETE
    196 ngx.HTTP_OPTIONS  
    197 ngx.HTTP_MKCOL    
    198 ngx.HTTP_COPY      
    199 ngx.HTTP_MOVE     
    200 ngx.HTTP_PROPFIND 
    201 ngx.HTTP_PROPPATCH 
    202 ngx.HTTP_LOCK 
    203 ngx.HTTP_UNLOCK    
    204 ngx.HTTP_PATCH   
    205 ngx.HTTP_TRACE  
    206 HTTP status constants    ngx.HTTP_OK (200)
    207 ngx.HTTP_CREATED (201)
    208 ngx.HTTP_SPECIAL_RESPONSE (300)
    209 ngx.HTTP_MOVED_PERMANENTLY (301)
    210 ngx.HTTP_MOVED_TEMPORARILY (302)
    211 ngx.HTTP_SEE_OTHER (303)
    212 ngx.HTTP_NOT_MODIFIED (304)
    213 ngx.HTTP_BAD_REQUEST (400)
    214 ngx.HTTP_UNAUTHORIZED (401)
    215 ngx.HTTP_FORBIDDEN (403)
    216 ngx.HTTP_NOT_FOUND (404)
    217 ngx.HTTP_NOT_ALLOWED (405)
    218 ngx.HTTP_GONE (410)
    219 ngx.HTTP_INTERNAL_SERVER_ERROR (500)
    220 ngx.HTTP_METHOD_NOT_IMPLEMENTED (501)
    221 ngx.HTTP_SERVICE_UNAVAILABLE (503)
    222 ngx.HTTP_GATEWAY_TIMEOUT (504) 
    223 Nginx log level constants    ngx.STDERR
    224 ngx.EMERG
    225 ngx.ALERT
    226 ngx.CRIT
    227 ngx.ERR
    228 ngx.WARN
    229 ngx.NOTICE
    230 ngx.INFO
    231 ngx.DEBUG
  • 相关阅读:
    Bootstrap 学习笔记 之网格系统
    自定义属性建立匹配、对应关系——JS学习笔记2015-5-27(第40天)
    CSS那些事儿——关于如何提高编写代码的能力
    自定义属性,匹配数组内容——JS学习笔记2015-5-26(第39天)
    css学习之幻灯片(轮播图)原理
    自定义属性,第一节——JS学习笔记2015-5-25(第38天)
    字体不同,造成的显示异常
    浮动相关理解,以及清除浮动的方法总结
    border实现三角箭头
    go mode
  • 原文地址:https://www.cnblogs.com/zf-crazy/p/14199932.html
Copyright © 2011-2022 走看看