Vickie Jack
Vickie Jack

Reputation: 95

How to connect to a web service that returns JSON in LUA

I am beginner in LUA. I have written the following code in lua file that mysql proxy run with it.

function read_query(packet)
    if string.byte(packet) == proxy.COM_QUERY then
        local command = string.lower(packet)
        if string.find(command, "select") ~= nil and string.find(string.lower(packet), "from") ~= nil then
            local socket = require('socket')
            local conn, err = socket.connect('localhost', 5050)
            print(conn, err)
            proxy.response.type = proxy.MYSQLD_PACKET_OK
            //proxy.response.resultset get json from web service (url)
            proxy.response.resultset = {
                fields = {
                    { type = proxy.MYSQL_TYPE_INT, name = "id", },
                },
                rows = {
                    { 9001 }
                }
            }
            return proxy.PROXY_SEND_RESULT
        end
    end
end

I want to connect to the web service on Port 5050 that return the JSON file and save the json that it returns in the proxy.response.resultset. Another question, How can i add socket module. I paste files like following image

socket module files

but give an error : can not find /socket/core.lua.

Upvotes: 0

Views: 345

Answers (1)

Paul Kulchenko
Paul Kulchenko

Reputation: 26744

local socket = require('socket')

You are using luasocket and it comes as a mix of lua (socket.lua) and binary (socket/core.so) files. You need to set (if it's not set already) to point to .so files; something like this may work: package.cpath=package.cpath..';./?.so'

Upvotes: 0

Related Questions