S
S
smilexcasper2017-07-14 21:57:09
Mobile development
smilexcasper, 2017-07-14 21:57:09

How to parse an array in nginx - lua?

I get the response body from php via ngx.location.capture , where there is an array in the response:
['success' => true, 'key1' => 'value1'];
When I output ngx.say("body:", res.body), I get res.body:array|
How can I get the value of key1?
res.body.key1,res.body.["key1"],res.body[0] - don't help
nginx.conf:

location /IdentityAccess/loginService {
            rewrite ^/IdentityAccess^/ /$1 break;
            proxy_pass http://identityaccess/login;
            proxy_redirect off;
        }

        location /IdentityAccess/login {
        lua_need_request_body on;
        content_by_lua '
        local res = ngx.location.capture("/IdentityAccess/loginService",{ method = ngx.HTTP_POST,body=ngx.var.request_body })
        if res then
        nxg.var.resbody = res.body
        ngx.say("status: ", res.status)
        ngx.say("body:", res.body)
        else
        ngx.say("error")
        end
        ';
        }

Answer the question

In order to leave comments, you need to log in

3 answer(s)
S
Sergey Lerg, 2017-07-14
@Lerg

Must be
or
print(res.body['key1'])

S
smilexcasper, 2017-07-14
@smilexcasper

Tried:
key = res.body.key1 and key = res.body.["key1"]
ngx.say("body:", key)

S
silverjoe, 2017-07-15
@silverjoe

Maybe the documentation will help?
https://github.com/openresty/lua-nginx-module#ngxl...
Output for debugging, everything that ngx.location.capture receives - variables and types and sort it out, maybe there is a table in the table and also in the table

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question