nginxlualua-tableopenresty

Lua NGINX How to get only one value from json POST body


Hello I have lua nginx and I'm having request body

My POST body is this :

{
    "param1": "1033893",
    "param2": "337483",
    "param3": "test",
    
}

I want to cut only param2 and make it a variable, then match it with a file which it's having lines that contain numbers like this.

If it's found then I should call another command

Update 1 :

I'm using

local check_body = ngx.req.get_body_data()

Update 2:

In bash I'm using this to cut the part and value of param2

echo $CURL_post_request | cut -d\| -f 1| grep -m1  -oP '\s*"param2"\s*:\s*\K[^,]+'| tr -d '"'| tr -d '}'

But in lua I'm not sure how to cut and put as variable?


Solution

  • I've been using another module http://dkolf.de/src/dkjson-lua.fsl/home

    First i'm doing adding entry to nginx.conf

    lua_package_path "/usr/local/openresty/lualib/?.lua;;";
    
    
      json = require "dkjson"
       local json_parse = json.decode(check_body)
       local iccid_cut = json_parse.param2