'mod_lua - Parsing a POST request body

I'm trying to parse a POST request using apache and mod_lua, i have something like this :

function authz_bulk_get(r, id_dash)
        r:debug("BULK_DEBUG START")
        if r.method == 'GET' then
                for k, v in pairs(r:parseargs()) do
                        r:debug("BULK_DEBUG " .. k .. " " .. v)
                end
        elseif r.method == 'POST' then
                r:debug("BULK_DEBUG TREAT POST")
                local jv_content = r:requestbody()
                r:debug("BULK_DEBUG REQUEST_BODY")
                r:debug("BULK_DEBUG " .. jv_content)
                jv_content = jv_content:gsub("%[", "")
                jv_content = jv_content:gsub("%]", "")
                local jv_jsonparse = json.decode(jv_content)
                r:debug("BULK_DEBUG " .. jv_content)
                r:debug("BULK_DEBUG " .. jv_jsonparse["type"])
                r:debug("BULK_DEBUG " .. jv_jsonparse["id"])
        end
        r:debug("BULK_DEBUG END")
        return apache2.AUTHZ_GRANTED
end

Everything seems OK in debug logs, i have all correct traces and I see requestbody and json parsing executing well but the response is {"statusCode":400,"error":"Bad Request","message":"[request body]: expected value of type [array] but got [null]"}

It looks like the request is emptied when i do r:requestbody() Maybe I need to re-transmit the POST request after doing this processing ?

Any ideas ?

Thanks



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source