krakend / krakend-lua

a lua interpreter for the KrakenD framework
Apache License 2.0
7 stars 14 forks source link

Can not read integer from response with Lua #15

Closed Lajule closed 3 years ago

Lajule commented 3 years ago

Can not read integer from response with Lua you can not read an integer in a response with lua script

Your configuration file The content of your krakend.json. When using the flexible configuration option, the computed file can be generated using FC_OUT=out.json

{
    "version": 2,
    "name": "krakend",
    "port": 8081,
    "timeout": "3000ms",
    "cache_ttl": "300s",
    "extra_config": {
    "github_com/devopsfaith/krakend-gologging": {
        "level":  "DEBUG",
        "prefix": "[KRAKEND]",
        "syslog": false,
        "stdout": true
    }
    },
    "endpoints": [
    {
        "endpoint": "/public/contacts",
        "headers_to_pass":["*"],
        "querystring_params":["*"],
        "backend": [
        {
            "host": ["http://127.0.0.1:8002"],
            "url_pattern": "/contacts/humans?field=firstname",
            "group": "contacts"
        }
        ],
        "extra_config": {
        "github.com/devopsfaith/krakend-lua/proxy": {
            "sources": [
            "lua/contacts.lua"
            ],
            "post": "post_backend(response.load())",
            "live": true,
            "allow_open_libs": true
        }
        }
    }
    ]
}

with contacts.lua:

function post_backend(resp)
   local data = resp:data()
   local contacts = data:get("contacts")

   print(contacts:get("count"))
end

http response:

{ contacts: { count: 1 } }

but got "nil" in logs

tobley commented 3 years ago

Might be due to JSON decoder setting UseNumber which unmarshals as a Number instead of float64.

When tableGet grabs data for the json number value its type is a json.Number instead of a float64. Since json.Number isn't a case in the switch statement, it falls through and returns nil.

Lajule commented 3 years ago

Ok but how I can get the value ?

kpacha commented 3 years ago

@tobley that sounds pretty logical!!! I didn't think about that!

I'll try to add the proposed fix, thanks for the great pointer

Lajule commented 3 years ago

;-) regards

github-actions[bot] commented 2 years ago

This issue was marked as resolved a long time ago and now has been automatically locked as there has not been any recent activity after it. You can still open a new issue and reference this link.