in kong/kong/pdk/request.lua [755:828]
function _REQUEST.get_body(mimetype, max_args)
check_phase(before_content)
local content_type = mimetype or _REQUEST.get_header(CONTENT_TYPE)
if not content_type then
return nil, "missing content type"
end
local content_type_lower = lower(content_type)
do
local s = find(content_type_lower, ";", 1, true)
if s then
content_type_lower = sub(content_type_lower, 1, s - 1)
end
end
if find(content_type_lower, CONTENT_TYPE_POST, 1, true) == 1 then
if max_args ~= nil then
if type(max_args) ~= "number" then
error("max_args must be a number", 2)
elseif max_args < MIN_POST_ARGS then
error("max_args must be >= " .. MIN_POST_ARGS, 2)
elseif max_args > MAX_POST_ARGS then
error("max_args must be <= " .. MAX_POST_ARGS, 2)
end
end
req.read_body()
local pargs, err = req.get_post_args(max_args or MAX_POST_ARGS_DEFAULT)
if not pargs then
return nil, err, CONTENT_TYPE_POST
end
return pargs, nil, CONTENT_TYPE_POST
elseif find(content_type_lower, CONTENT_TYPE_JSON, 1, true) == 1 then
local body, err = _REQUEST.get_raw_body()
if not body then
return nil, err, CONTENT_TYPE_JSON
end
local json = cjson.decode(body)
if type(json) ~= "table" then
return nil, "invalid json body", CONTENT_TYPE_JSON
end
return json, nil, CONTENT_TYPE_JSON
elseif find(content_type_lower, CONTENT_TYPE_FORM_DATA, 1, true) == 1 then
local body, err = _REQUEST.get_raw_body()
if not body then
return nil, err, CONTENT_TYPE_FORM_DATA
end
local parts = multipart(body, content_type)
if not parts then
return nil, "unable to decode multipart body", CONTENT_TYPE_FORM_DATA
end
local margs = parts:get_all_with_arrays()
if not margs then
return nil, "unable to read multipart values", CONTENT_TYPE_FORM_DATA
end
return margs, nil, CONTENT_TYPE_FORM_DATA
else
return nil, "unsupported content type '" .. content_type .. "'", content_type_lower
end
end